Skip to main content

How to Format a Number in 2 Decimal Places and Grouped Thousands in PHP

The number format shown above is commonly used in many countries to make it easier for people to read and understand large numbers. The number is broken up into smaller units, each represented by a digit, and then the value of each unit is multiplied by a power of 10. This creates a number that is easy to understand and work with.

In PHP, there are a number of functions that can be used to format a number in this way. We will sue number_format() in this snippet.

The number_format() function is the easiest way to format a number in PHP. This function takes up to four parameters: the number to be formatted, the number of decimal places to use, the decimal separator character, and the thousands of separator characters.

$number = "1000.0001";
echo number_format($number, 2, '.', '');
//output: 1000.00
echo number_format($number, 2, '.', ',');
//output: 1,000.00

By continuing to use the site, you agree to the use of cookies.