Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP number format

I have this string :

000000000000100

and need to convert it to:

1,00

So, the rules are:

  1. Divide the number by 100 and use a comma as decimal separator
  2. Strip leading zeros
  3. Keep two decimals
like image 377
Ste Avatar asked Jun 10 '11 15:06

Ste


1 Answers

From the PHP Manual page on number_format:

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

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

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

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

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

The zeros are automatically removed by PHP when converting the string to a number.

like image 198
Lekensteyn Avatar answered Oct 19 '22 15:10

Lekensteyn