Jump to content


This topic is now archived and is closed to further replies.


Formatting Number

Recommended Posts

I'm trying to format a number to 2 decimal places like currency but when I apply this to a number with a comman in it like 12,233 I get 12.00?  I obviously don't understand how this is working since a comma isn't period and it is rounding.  How can I insure that I'm always getting a two decimal place number.  I used money format but it was dropping the decimal places.  I'm looking for a number like 12,233.00 above.  Thanks

Share this post

Link to post
Share on other sites
use number format: like this:
[code=php:0]$number = '12,589';

// remove any spaces/commas
$number = str_replace(array(',', ' '), '', $number);

$number2 = number_format($number, 2, '.', ',');

echo $number2;[/code]

European countries such as france, spain etc use a comma (,) as a decimal seperator rather than a period (.). SO number format is rounding the number. What you should do is remove any commas/spaces from the number. Then pass the number into the format_number function.

Share this post

Link to post
Share on other sites
you need to strip the comma out before you run your format on it. in PHP, numbers [b]do not[/b] contain commas, so you've got to try to get it down to the digits only if possible:
$num = "12,233";
$num = preg_replace('|,|', '', $num);
echo number_format($num, 2);

hope that helps

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.