PHP number format

I have this line:

000000000000100

and you need to convert it to:

1,00

So, the following rules:

  • Divide the number by 100 and use a comma as the decimal separator
  • Upper Stripes
  • Keep two decimal places
+3
source share
2 answers

On the PHP Manualnumber_format page at :

string number_format (float $ number, int $ decimals = 0, string $ dec_point = '.', string $ thousand_sep = ',')

If you want type numbers to be 123456formatted as 1234,45, use:

echo number_format($number / 100, 2, ",", "");

If you need a dot as a thousands separator ( 1.234,56):

echo number_format($number / 100, 2, ",", ".");

PHP automatically removes zeros when converting a string to a number.

+15
source
string number_format ( float $number , 
                       int $decimals = 0 ,
                       string $dec_point = '.' ,
                       string $thousands_sep = ',' )

: http://php.net/manual/en/function.number-format.php

// divide by 100 to shift ones place.
echo number_format((int)'000000000000100' / 100,2,',','');
+6

All Articles