PHP number_format gotcha

Image for PHP number_format gotcha from

By Andrew McCombe
December 19, 2009

Be careful when doing any kind of calculations with numbers that have been passed through PHP's number_format function as they can lead to unexpected results. The problems lie with the thousands separator (or the decimal separator if you use anything other than '.'). Here's an example:

[email protected]:/$ php -a
Interactive shell

php > $number = 1234.56;
php > echo $number;
php > $number = number_format($number, 2);
php > echo $number;
php > echo $number * 100;
php >

The issue is that the calculation ignores everything after the thousands separator ' , '.