PHP number_format()
Published Sep 17, 2023
Contribute to Docs
The number_format() function takes a given number and formats it with grouped thousands. If a decimal is given, the number will be rounded up.
Syntax
number_format($number, $decimals, $decimal_point, $separator);
$number: The number to format.$decimals: Optional parameter that sets the number of digits after the decimal point. If not given, the number returned will be a whole number.$decimal_point: Optional parameter that specifies the decimal separator and should be given as a string.$separator: Optional parameter that specifies the thousands separator and should be given as a string.
Example
This example uses the number_format() function to format a whole number using "," as a decimal separator and "x" as a thousands separator.
<?phpecho number_format("1000000",2,",","x");?>
This will result in the following output:
1x000x000,00
Codebyte Example
The following codebyte example can be run and calls the number_format() method two times. The second call uses the optional $decimals, $decimal_point, and $separator parameters.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn PHP on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours