John Davidson

php - iOS/Safari cutting trailing zero BEFORE decimal point

0 comments
Message:


This one is mind boggling. I have tried several ways to keep the 0 before the decimal point, however only in Safari/iOS is this being cut, making it difficult to determine.


I am trying to display the max price of a variation product on the archive page. This (as well as several other approaches, note - the commented out number_format approach) works perfectly fine on Windows or Android, using Firefox, Chrome and IE.


However, on iOS / Safari, numbers such as 150 are coming back as 15. The last 0 is also being trimmed, despite it being BEFORE the decimal point.


The var_dump() are both returning strings. The raw returns 150.00 as expected, while the $price variable is returning 150, as desired... on desktop and Android only.


$raw_price = $product->get_variation_price( 'max', true );
//$price = number_format($raw_price, 0, '.', ' ');
$price = preg_replace( '/' . preg_quote( wc_get_price_decimal_separator(), '/' ) . '0++$/', '', $raw_price );

echo '<div class="corner">$' . $price . '</div>';
var_dump($price);
var_dump($raw_price);

I should mention that I am using the built in filter to trim zeros site-wide, used below:


add_filter( 'woocommerce_price_trim_zeros', '__return_true' );

Which may potentially be affecting things? I can't see why, however I can't find a solution to this one.



My Answer:
>get_price();
$price = number_format($raw_price, 2, '.', '');

echo $raw_price; // Outputs 150.00
echo $price; // Outputs 150

// You can try using sprintf to format the number instead
$price = sprintf("%.2f", $raw_price);

echo $price; // Outputs 150.00

// If the above solutions don't work, you can try explicitly casting the price to a string
$price = (string) $raw_price;

echo $price; // Outputs 150.00

// If none of the above solutions work, you may need to look into browser-specific issues with number formatting in PHP and JavaScript.

Rate this post

3 of 5 based on 1481 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us