PHP Number Format Example
Sometimes it can be neccessary to display a nicely formatted number on a webpage, instead of an ugly and hard to understand number. For example 234,654.49 is way more beautiful and easier to comprehend than 234654.49876. This is where number formating comes in. In this example we shall discuss number formatting in PHP.
For this example we will use:
- A computer with PHP >= 5.5 installed
- notepad++
1. Getting Started
Nicely formatted number are pretty, easier to read and assimilate better than an unformatted number. For this reason it is always better to nicely format our numbers before they are displayed on our webpage or even saved in a database. PHP provides us with a reliable function to do this.
The number_format()
function formats a number with group thousands. Its syntax is below:
string number_format ( float $number , int $decimals , string $dec_point, string $thousands_sep )
This function only accepts either one, two, or four parameters (not three). If just one parameter is supplied,the number will be formatted without decimals, but with a comma (“,”) between every group of thousands.
If two arguements are supplied, the number will be formatted with decimals. Decimals with a dot (“.”) in front, and a comma (“,”) between every group of thousands.
If all four arguements are supplied, number will be formatted with decimals decimals, dec_point instead of a dot (“.”) before the decimals and thousands_sep instead of a comma (“,”) between every group of thousands.
The function will return a string representing the formatted number.
$number
is the first parameter and the only required parameter and it represents the number to be formatted. If only this parameter is supplied the number will be formatted without decimals and with comma (,) as the thousands separator.$Decimals
is an optional parameter, it specifies how many decimal. If this parameter is set, the number will be formatted with a dot (.) as decimal point.$dec_point
Is an optional parameter. It specifies what string to use for decimal point.$thousands_sep
is an optional parameter it sets the thousands separator.
If this parameter is given, all other parameters are required as well
This function is supported by PHP 4, PHP 5 and PHP 7.
1.1 Examples
index.php
<!DOCTYPE html> <html lang="en"> <head> <title>PHP Number Format Example</title> </head> <body> <?php // returns 234,654 echo number_format("234654.49876")."<br>"; ?> </body> </html>
A very simple example with only one parameter supplied to the number_format
function.
Note: 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.
index2.php
<!DOCTYPE html> <html lang=eng> <head> <style> #map { height: 400px; width: 100%; } </style> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0"/> <body> <?php // returns 234,654.50 echo number_format("234654.49876",2)."<br>"; ?> </body> </html>
In the script above we supply the second parameter.
index3.php
<!DOCTYPE html> <html lang=eng> <head> <style> #map { height: 400px; width: 100%; } </style> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0"/> <body> <?php // returns 234.654,50 echo number_format("234654.49876",2,",",".")."<br>"; ?> </body> </html>
2. Summary
In this example we learnt about number formatting in PHP. We learnt how to use PHP number_format
function.
3. Download the source code
You can download the full source code of this example here: phpnumberformatexample