Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add commas to numbers in PHP

Tags:

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?

like image 237
Ahmad Fouad Avatar asked Jan 03 '11 06:01

Ahmad Fouad


People also ask

How do you put commas in numbers?

What are the rules for using commas? First comma is placed three digits from the right of the number to form thousands, second comma is placed next two digits from the right of the number, to mark lakhs and third comma is placed after another next two digits from the right to mark crore, in Indian system of numeration.

How can I set 2 decimal places in PHP?

$twoDecNum = sprintf('%0.2f', round($number, 2)); The rounding correctly rounds the number and the sprintf forces it to 2 decimal places if it happens to to be only 1 decimal place after rounding. Show activity on this post. This will give you 2 number after decimal.

How do you write 1000000 with commas?

A comma is placed every third digit to the left of the decimal point and so is used in numbers with four or more digits. Continue to place a comma after every third digit. For example: $1,000,000 (one million dollars)

What is number format PHP?

The number_format() function is an inbuilt function in PHP which is used to format a number with grouped thousands. It returns the formatted number on success otherwise it gives E_WARNING on failure. Syntax: string number_format ( $number, $decimals, $decimalpoint, $sep )


1 Answers

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

like image 112
andrewk Avatar answered Oct 08 '22 18:10

andrewk