Number format in PHP

How to comfortably format numbers in PHP through the number_format() function that allows you to indicate the number of decimal places, as well as the separation between the integer part and the decimal or the thousands separation.

When we display a number as content on a web page we may generally need to use a specific format. For example, have only two decimal placesor that uses commas -or points- to separate decimals, as well as the separate units of thousands. Typical number formats could be:

1,000,505.56 5003.60 5,000.00

Presenting the numbers with one or another format is simple, since in PHP there is a specific function to format numbers, which depending on the parameters received will format them in one way or another. In this article we are going to learn about this function, as well as its configuration possibilities, exploring its possibilities through several examples.

number_format() function

The formatting of numbers will be carried out with the PHP function number_format(). This function takes one, two, or four parameters. That is, we have these restrictions:

  • There is only one required parameter (the number we want to format).
  • The second parameter is optional, just like the third and fourth
  • But if we specify the third parameter, we are forced to specify the fourth as well.

Let’s take a closer look at the parameters of PHP’s number formatting function, with several examples.

Parameter 1 number_format, the number:

The first parameter is the number to format. As we said, although it is obvious, it is always a necessary parameter.

See also  Create a blob from a canvas

$number = 15200.67; number_format($number); // returns 15,201

In this case, formatting the number will give us the number without decimals and with a comma as a thousands separator. This format is the one used in English (English-speaking people separate thousands with commas when writing numbers), which surely won’t work for us developers who work in Spanish.

Note that the number_format() function has also rounded off the decimal places that it is not displaying. We will see this rounding well in this example:

$number = 999999999.99; number_format($number); //returns 1,000,000,000

Parameter 2 number_format, the decimal places:

With the second parameter, which is optional, we indicate the number of decimal places that we want to appear in the formatted number.

$number = 15200.67; number_format($number,2); //returns 15,201.67

As we can see, in this case two decimals have been incorporated into the number format. Use commas to separate thousands and a period to separate thousands. As we can see, it continues to use English notation to format numbers.

Another example, in which we can see that the number is always rounded, if the decimals to be displayed are less than those of the original number.

$number = 1885200.89; number_format($number,1); //returns 1,885,200.9

Parameters 3 and 4 for formatting decimal separators and thousands units

The last parameters, which we must always use together, serve to specify the separators that we want to use for decimals and units of thousands. If we want to format the numbers with the Spanish notation, we would necessarily have to use these parameters.

For example, this is how we would format the numbers in Spanish:

  • Separate decimals with commas
  • Separate units of thousands with a period.
See also  Angular CLI

$number = 1002002.365; number_format($number, 2, “,”, “.”); //returns 1,002,002.37

If, for example, the thousands are not separated, we simply pass the empty string as the thousands unit separator (fourth parameter):

$number = 9540.2; number_format($number, 2, “,”, “”); //returns 9540.20

Conclusion on formatting numbers with PHP

That’s all we need to know to format numbers in php. As you have seen, it is very easy to present the numbers with the decimals that we need and with the correct separators for decimals and thousands for the Spanish language. With a few changes we could also do it for other languages ​​like English, in fact this option would only require omitting the last two arguments of the function call.

Loading Facebook Comments ...
Loading Disqus Comments ...