Skip to content
Advertisement

How can I add commas to numbers in PHP

I would like to know how can I add comma’s to numbers. To make my question simple.

I would like to change this:

1210 views

To:

1,210 views

and :

14301

to

14,301

and so on for larger numbers. Is it possible with a php function?

Advertisement

Answer

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

I’m assuming you want the english format.

<?php

$number = 1234.56;

// english notation (default)
$english_format_number = number_format($number);
// 1,235

// French notation
$nombre_format_francais = number_format($number, 2, ',', ' ');
// 1 234,56

$number = 1234.5678;

// english notation with a decimal point and without thousands seperator
$english_format_number = number_format($number, 2, '.', '');
// 1234.57

?>

my 2 cents

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement