Reputation: 32721
I have a variable $totalprice
with number 1,072.00
.
If I use int($totalprice)
or number_format($totalprice)
, it outputs 1.00
.
How can I solve this problem? I want 1072.00
.
Upvotes: 0
Views: 156
Reputation: 1272
It is not a bug ,because your number is parsed from left till first invalid character (the ,)
so 1,072.00 is really 1,072.00 , and just the bold section is parsed when you are trying to convert the string to number.
Upvotes: 0
Reputation: 1706
Problem you are having is about locales. To avoid issue about money formatting i would suggesT you to use
string money_format ( string $format , float $number )
By using this function you can format the number according to desired format. you can read more about this function and what formatting type to use here: money_format
Upvotes: 1
Reputation: 124758
$totalprice = (float)str_replace(',', '', $totalprice)
That will get you a normal float representation of the number. You can then apply the number_format function to that to get the desired output.
Perhaps you should rethink your handling of numbers. How in the first place the number gets the format 1,072.00
? You shouldn't do any formatting to it until outputting to the browser, so the number should always be in the 1072.00
format.
Upvotes: 2