This article will explain how to use the PHP number_format() function, with some helpful examples.
Different regions use different number formatting – Some countries, including the UK, USA, Australia, and New Zealand, use a comma (,) to separate thousands when writing large numbers and use a period (.) as a decimal separator.
Your computer most likely uses the same formatting when storing number values in a database.
However, some countries, like Germany, use a period as the thousands separator and a comma as the decimal separator. Others use a space as the thousands separator.
If you’re displaying numbers to or reading input from a user who is expected to be entering a number format that differs from the format you are using to store numbers, you need to convert back and forward.
That’s what the PHP number_format function is for!
PHP number_format Syntax
The syntax for the number_format() function is as follows:
number_format($number, $decimals, $decimal_separator, $thousands_separator)
- $number is the number, or variable containing the number you wish to format
- This value is required
- $decimals is the number of decimal places to display in the formatted number
- If 0, the decimal_separator will not be shown in the formatted number
- Defaults to 0 if not specified
- Rounds the number – if the number has more decimal places specified than the given format, the formatted number returned will be rounded
- $decimal_separator and $thousands separator are the characters you wish to use as a decimal place separator and thousands unit separator, respectively
- They are optional, and if not supplied, a period (.) will be used as the $decimal_separator and a comma (,) as the $thousands separator
- number_format() will return a string containing the formatted number
Here are some examples of how the PHP number_format() function can be used.
By default, number_format() will use English number formatting, with a period used as the decimal separator and a comma used as a thousand separator:
$myNumber = 4567.375; $myFormattedNumber = number_format($myNumber); echo $myFormattedNumber; // Will print 4,567
Note that as the function defaults to 0 decimal places, no decimal separator or decimal digits are present, and the number has been rounded.
Let’s apply some custom formatting – using a space instead of a comma as the thousands separator, and setting the number of decimal places to display to 2:
$myNumber = 4567.375; $myFormattedNumber = number_format($myNumber, 2, '.', ' '); echo $myFormattedNumber; // Will print 4 567.38
Note again that the formatted number has been rounded to the required number of decimal places.
It’s important to be consistent in what format you store numbers. If they are not consistent and formats are mixed, your data will become useless – you won’t know which numbers were stored in which format.
It’s also a useful tool when displaying currency values – different currencies have different formats. PHP formerly had the money_format() function for this, but it has since been removed in recent versions of PHP. number_format() is the worthy replacement.