Formatting Numbers with PHP's number_format() Function

Learn how to use number_format() to display numbers to the user in an easy-to-read format, including thousands separators.

When displaying a number in a Web page, it's nice to format the number in an easy-to-read way. For example, 123,456.78 is nicer to read than 123456.784321.

PHP's number_format() function gives you an easy way to format numbers for displaying to the user. You can separate thousands with commas or other separators, choose the decimal point character, and choose the number of decimal places to display (or display no decimals at all).

Basic number formatting

In its most basic form, number_format() accepts a number to format, and returns a string containing the formatted number, rounded to the nearest whole number, with commas between each group of thousands:


$myNumber = 1234567.89;

// Displays "1,234,568"
echo number_format( $myNumber );

Since number_format() returns a string containing separator characters, you can't use the output of number_format() as a numeric value with other math operators or functions. Therefore, make sure you do all your calculations on the original numeric value, and only convert the value to a string with number_format() when you're ready to display it to the user.

Specifying the number of decimal places

To add decimal places to the formatted number, specify the number of decimals to add as the second argument. number_format() rounds the number up or down to the specified number of decimals:


$myNumber = 123456.784321;

// Displays "123,456.78"
echo number_format( $myNumber, 2 );

// Displays "123,456.8"
echo number_format( $myNumber, 1 );

Using different separators

By default, number_format() uses a dot for the decimal point, and a comma for the thousands separator. To use different separators, pass the new decimal point separator as the third argument, and the new thousands separator as the fourth argument.

The following example displays a number using the French convention — a comma for the decimal point, and a space for the thousands separator:


$myNumber = 123456.784321;

// Displays "123 456,78"
echo number_format( $myNumber, 2, ',', ' ' );

If you wanted to display a number in English notation rounded to 2 decimal places, but without thousands separators, you could write the following:


$myNumber = 123456.784321;

// Displays "123456.78"
echo number_format( $myNumber, 2, '.', '' );

You can use PHP's localeconv() function to find out the computer's current locale settings for the decimal point and thousands separator — handy for plugging into number_format().

Summary

number_format() gives you a simple way to display numbers in a nice, easy-to-read format. You've learned how to call number_format(), how to specify the number of decimals to display, and how to display numbers using a different thousands separator and decimal point.

Incidentally, if you need to display a number using words — for example, "one thousand two hundred and thirty-four" — check out the Numbers_Words PEAR module.

Happy coding!

Learn PHP With Ease!

Written by Matt Doyle — ELATED's resident Web programming expert — Beginning PHP 5.3 is a complete introduction to PHP, covering everything in these tutorials and lots more besides. Find out how to:

  • Set up PHP on your computer
  • Use strings, arrays, functions and objects
  • Create interactive Web forms
  • Handle cookies and sessions
  • Work with files on the server
  • Build database-driven sites with MySQL
  • Send emails from your scripts
  • Create images on the fly with PHP
  • Work with regular expressions
  • Write robust, secure PHP applications

...and lots more!

“What a pleasure it's been spending hours and hours studying PHP with this magical book.” — Lulio, Florida
“The book is not only great for learning, but I find myself using it constantly as a reference as well!” — David A. Stoltz

Buy Beginning PHP 5.3 now from Amazon.comBeginning PHP 5.3 or Amazon.co.ukBeginning PHP 5.3.

Follow Elated

Related articles

Responses to this article

There are no responses yet.

Post a response

Want to add a comment, or ask a question about this article? Post a response.

To post responses you need to be a member. Not a member yet? Signing up is free, easy and only takes a minute. Sign up now.

Top of Page