/** * Ensure floats are correctly converted to strings based on PHP locale * * @param null $check * @param int $object_id * @param string $meta_key * @param mixed $meta_value * @param mixed $prev_value * @return null|bool */ public static function update_post_metadata($check, $object_id, $meta_key, $meta_value, $prev_value) { if (!empty($meta_value) && is_float($meta_value) && in_array(get_post_type($object_id), array_merge(wc_get_order_types(), array('shop_coupon', 'product', 'product_variation')))) { // Convert float to string $meta_value = wc_float_to_string($meta_value); // Update meta value with new string update_metadata('post', $object_id, $meta_key, $meta_value, $prev_value); // Return return true; } return $check; }
private function formatFloat($value, $zeroReplacement = '') { if ($value == 0) { return $zeroReplacement; } return wc_float_to_string($value); }
/** * Test wc_float_to_string(). * * @since 2.2 */ public function test_wc_float_to_string() { // given string, return string $this->assertEquals('1.99', wc_float_to_string('1.99')); $this->assertEquals('1.17', wc_float_to_string(1.17)); }