PHP: Format Numbers with Thousands Separator

Sandra Jun 29, 2026

Formatting Numbers with Thousand Separators in PHP

How to format numbers in thousands in Excel
How to format numbers in thousands in Excel

When working with numbers in PHP, it's often necessary to format them for better readability. One common formatting requirement is to add a thousand separator, such as a comma, to large numbers. This article will guide you through various methods to achieve this in PHP.

the back side of a computer screen with numbers and symbols in white on blue background
the back side of a computer screen with numbers and symbols in white on blue background

Using number_format() Function

The most straightforward way to add a thousand separator to numbers in PHP is by using the built-in number_format() function. This function takes four arguments: the number to format, the number of decimals, the decimal point separator, and the thousands separator.

Google Image Result for https://cdn1.knowledgeboat.com/img/apc10/chap_4-p26a_2.jpg
Google Image Result for https://cdn1.knowledgeboat.com/img/apc10/chap_4-p26a_2.jpg

Basic Usage

Here's a basic example of using number_format() to add a comma as a thousand separator:

the numbers are displayed in blue and black on this white background, as well as an arrow
the numbers are displayed in blue and black on this white background, as well as an arrow

echo number_format(1234567, 0, '.', ','); // Outputs: 1.234.567

Customizing Separators

You can customize the separators by changing the third and fourth arguments. For example, to use a dot as the decimal point and a space as the thousand separator, you can do:

echo number_format(1234567.89, 2, '.', ' '); // Outputs: 1 234 567.89

Using Internationalization (i18n)

projecteuler.net #9 in JavaScript
projecteuler.net #9 in JavaScript

PHP provides internationalization (i18n) features that can help format numbers according to the locale settings. The localeconv() function can retrieve the locale's numeric formatting information, which includes the thousand separator.

Using localeconv()

Here's an example of using localeconv() to get the thousand separator and then using it with number_format():

an array of numbers and symbols in the form of a computer code, with many digits
an array of numbers and symbols in the form of a computer code, with many digits

$locale = localeconv();
echo number_format(1234567, 0, $locale['decimal_point'], $locale['thousands_sep']); // Outputs: 1,234,567

Using Regular Expressions

If you prefer not to use built-in functions, you can also add a thousand separator to numbers using regular expressions. Here's a simple function that does this:

a spreadsheet showing the number and type of items for each item in this document
a spreadsheet showing the number and type of items for each item in this document
an array of numbers that are written in different languages
an array of numbers that are written in different languages
two tables with numbers on them and one has the same number in each column, which is
two tables with numbers on them and one has the same number in each column, which is
How to Format Numbers in Excel Step by Step
How to Format Numbers in Excel Step by Step
an image of a poster with numbers in yellow and black on the front, as well as
an image of a poster with numbers in yellow and black on the front, as well as
an image of a table with numbers and symbols on it, which are labeled in the following
an image of a table with numbers and symbols on it, which are labeled in the following
an image of some type of programming program
an image of some type of programming program
a sheet of paper with numbers and dates on it in front of a computer screen
a sheet of paper with numbers and dates on it in front of a computer screen
an image of a table with numbers and symbols on it in the form of a chart
an image of a table with numbers and symbols on it in the form of a chart
a large number of numbers on a sheet of paper with green and blue numbers in the middle
a large number of numbers on a sheet of paper with green and blue numbers in the middle
the list of different types of electronic devices and their corresponding numbers are shown in this table
the list of different types of electronic devices and their corresponding numbers are shown in this table
a computer screen showing the number of files and directories in each file, as well as numbers
a computer screen showing the number of files and directories in each file, as well as numbers
the numbers are black and white in color
the numbers are black and white in color
two tables with numbers and the same number in each table, one has an odd place for
two tables with numbers and the same number in each table, one has an odd place for
I Will Crawl Read Sec Filings
I Will Crawl Read Sec Filings
Hex
Hex
How to Convert Numbers to Binary
How to Convert Numbers to Binary
HOUSE NUMBER NUMEROLOGY | Complete Guide (with Pictures)
HOUSE NUMBER NUMEROLOGY | Complete Guide (with Pictures)
Boston Excel Consulting
Boston Excel Consulting
the screenshoter is displaying numbers and symbols on their phone's display screen
the screenshoter is displaying numbers and symbols on their phone's display screen

Custom Function

The following function takes a number and a separator as arguments, then uses preg_replace() to insert the separator every three digits from the right:

function add_thousand_separator($number, $separator) {
    return preg_replace('/(\d)(?=(\d{3})+$)/', '$1' . $separator, $number);
}

echo add_thousand_separator(1234567, ','); // Outputs: 1,234,567

Comparison of Methods

Here's a comparison of the methods discussed above, using the number 1234567 and a comma as the thousand separator:

Method Result
number_format() 1,234,567
localeconv() with number_format() 1,234,567
Custom function with regular expression 1,234,567

All methods produce the same result, but they differ in complexity and flexibility. The choice between them depends on your specific needs and preferences.

In conclusion, PHP provides several ways to format numbers with thousand separators. Whether you prefer built-in functions, internationalization features, or custom regular expressions, you can easily add thousand separators to numbers in PHP to improve readability and usability.