"Mastering PHP: Formatting Numbers with number_format Function"

Sandra Jun 29, 2026

The PHP number_format function is an incredibly useful tool for developers, enabling them to format numeric values as strings with a specified number of decimal places and thousands separators. This function is particularly handy when dealing with large numbers or currency, as it enhances readability and understanding for users. In this article, we will delve into the intricacies of the PHP number_format function, exploring its syntax, parameters, and various use cases.

Using the is_numeric() Function in PHP
Using the is_numeric() Function in PHP

Understanding the PHP number_format Function

Using date function in PHP
Using date function in PHP

The PHP number_format function is a built-in function that takes a numeric value and formats it as a string with a specified number of decimal places and thousands separators. The function returns the formatted string, allowing developers to display numbers in a more user-friendly format.

Syntax and Parameters

Using the count() function in PHP
Using the count() function in PHP

The basic syntax of the PHP number_format function is as follows:

```php number_format ( float $number , int $decimals = 0 , string $dec_point = "." , string $thousands_sep = "," ) ```

The function accepts four parameters:

PHP Delimiters (Very Simple Examples)
PHP Delimiters (Very Simple Examples)
  • $number: The numeric value to be formatted.
  • $decimals: The number of decimal places to display. Default is 0.
  • $dec_point: The character to use as the decimal point. Default is a period (.).
  • $thousands_sep: The character to use as the thousands separator. Default is a comma (,).

Formatting Numbers with PHP number_format

Let's explore some practical examples of using the PHP number_format function to format numbers in various ways.

three types of number system with numbers in the bottom left corner and on top right
three types of number system with numbers in the bottom left corner and on top right

Formatting Currency

One of the most common use cases for the PHP number_format function is formatting currency. By specifying the appropriate number of decimal places and thousands separators, developers can display currency values in a clear and understandable format. Here's an example:

```php $price = 123456.789; echo number_format($price, 2, ".", ","); ```

This will output: 123,456.79

Using the getdate() function in PHP
Using the getdate() function in PHP

Formatting Large Numbers

When dealing with large numbers, it's essential to use a thousands separator to improve readability. The PHP number_format function makes this easy. Here's an example:

the secret code for numbers and symbols is shown in this graphic above it's description
the secret code for numbers and symbols is shown in this graphic above it's description
an image of a number system with numbers
an image of a number system with numbers
How to Use the rand() Function in PHP
How to Use the rand() Function in PHP
PHP Unlink Function with Example Program
PHP Unlink Function with Example Program
PHP include and include_once
PHP include and include_once
the types of numbers in python are shown on top of each other, and below it is
the types of numbers in python are shown on top of each other, and below it is
ms excel formula
ms excel formula
PHP mysql pdo basic
PHP mysql pdo basic
PHP Data Types
PHP Data Types
Not in Array PHP (with Advanced Program Examples)
Not in Array PHP (with Advanced Program Examples)
the text function is shown in green and white, as well as an image of numbers
the text function is shown in green and white, as well as an image of numbers
Math Functions
Math Functions
PHP Date Range
PHP Date Range
Functions in Python | Types, Example
Functions in Python | Types, Example
the numbers in python code and output are displayed on this page, which shows how to use
the numbers in python code and output are displayed on this page, which shows how to use
Advance Pagination with jQuery and PHP
Advance Pagination with jQuery and PHP
Simple Pagination With PHP MySQL
Simple Pagination With PHP MySQL
the diagram shows how to use python numbers for data types and their functions, as well as
the diagram shows how to use python numbers for data types and their functions, as well as
a sheet of paper with numbers and symbols in the form of an order number system
a sheet of paper with numbers and symbols in the form of an order number system
an image of a screen with text and numbers in different languages, including the same number
an image of a screen with text and numbers in different languages, including the same number

```php $largeNumber = 1234567890; echo number_format($largeNumber); ```

This will output: 1,234,567,890

Formatting Percentages

The PHP number_format function can also be used to format percentages. By specifying the number of decimal places, developers can control the precision of the percentage value. Here's an example:

```php $percentage = 0.56789; echo number_format($percentage, 2, ".", "") . "%"; ```

This will output: 56.79%

PHP number_format vs. printf and sprintf

While the PHP number_format function is specifically designed for formatting numbers, other functions like printf and sprintf can also be used to format numbers. However, number_format is generally preferred for its simplicity and readability. Here's a comparison:

Function Syntax Example Output
number_format number_format(123456.789, 2, ".", ",") 123,456.79
printf printf("%.2f", 123456.789) 123456.79
sprintf $formattedNumber = sprintf("%.2f", 123456.789); $formattedNumber = "123456.79"

As you can see, while printf and sprintf can format numbers, they do not add thousands separators by default and require more complex syntax to achieve the same results as number_format.

Conclusion

The PHP number_format function is an invaluable tool for developers, enabling them to format numbers in a clear and user-friendly manner. Whether you're dealing with currency, large numbers, or percentages, number_format has you covered. By understanding its syntax and parameters, you can harness the power of this function to enhance the readability and usability of your applications.