$price=number_format(round($preciosinr,0)-0.01,2,'.','');
echo $price;
This should work
質問
I have a doubt with how to use the number_format and round function together, this because I have an script to import all my supplier's products, but I need round the prices, for example:
Supplier's price: $1854.81 The price rounded: $1854.99 (This is the format that I want) The price that my script print: $1,854.90
I tryed 3 PHP variant to do this:
Variant #1:
$preciosinr = 1854.81;
echo number_format(round($preciosinr*4)/4,2); //Print 1,854.90
Variant #2
$preciosinr = 1854.81;
$pos = 3 - floor(log10($preciosinr));
echo number_format(round($preciosinr,$pos)-0.10,2); //Print 1,854.75
Variant #3
$preciosinr = 1854.81;
number_format($preciosinr,2);
number_format(round($preciosinr,1),2);
number_format(round($preciosinr,0),2);
echo number_format(round($preciosinr,0)-0.01,2); //Print 1,854.99
As you can see all the variants prints the price with "," and I need the price without this because my system detect the price incorrectly.
I read in php.net that I need use the following sintaxis but I don't know how can integrate with my code.
// english notation without thousands separator
$english_format_number = number_format($number, 2, '.', '');
// 1234.57
Can you help me?
解決
$price=number_format(round($preciosinr,0)-0.01,2,'.','');
echo $price;
This should work
他のヒント
I found it strange that the answer that was accepted is actually wrong.
Example case:
// The input we want to check
$stringNumber = '7616.95';
// As suggested by accepted answer
$formatted = number_format(round($stringNumber,0)-0.01,2,'.','' );
echo $formatted; // Outputs: 7616.99
A better solution is found here: https://stackoverflow.com/a/7459107