PHP: Number Format Function to Format Numbers in PHP (Integer or Float)

by Yang Yang on April 23, 2009

Share This Article:
Subscribe to Kavoir: blog feed

To display numbers to end users, especially large numbers, you may need to format them to be more user friendly. For example, with a large integer 43386000.75, it is usually preferred to comma-separate every 3 digits to this:

43,386,000.75

To format numbers this way in PHP, you need number_format() function:

print number_format(43386000.75); // prints '43,386,000.75'

This is English notation of numbers with thousands separator. For French number notation:

print number_format(43386000, 2, ',', ' '); // prints '43 386 000,75'

Deducing from the parameters used in the above example, we have the number_format() function synopsis:

string number_format ( float $number , int $decimals , string $dec_point , string $thousands_sep )

With number_format(), you can format any integer number of floating point number by thousands separator (which in most cases is a comma), decimal point (which usually is a period point), number of decimal digits after the decimal point.

Share This Article:
Subscribe to Kavoir: blog feed

You should also read:

{ 3 comments… read them below or add one }

deviant_two June 15, 2009 at 9:25 pm

how to make 0001 from 1 ? or from 94 to 0094 ? or like from 35.6 going tobe 0035.6 ?

$a=1;
$b=4;//4 digit max

ive my own function to create it, but some says its already build in php fucntion, what it is name?

Reply

Yang Yang June 15, 2009 at 10:29 pm

@deviant_two,

You can surely try printf() or sprintf() to achieve that.

http://www.php.net/printf
http://www.php.net/sprintf

Reply

Ravinder December 8, 2010 at 3:24 pm

Dear sir / madam

I have need to convert number to words in php.

Like 123 to convert in word please sent me the ans.

Reply

Leave a Comment

Previous post:

Next post: