Formatting number using the number_format() function in PHP

Today, we’ll show you how to format the number using the number_format() in PHP.

The number_format() is a PHP built-in function which is used to format the number of data. It returns the formatted value based on the parameter passed in function.

Syntax:

Parameters:

  • num (Required): It specifies the number to be formatted.
  • decimals (Optional): It specifies how many decimals.
  • decimalPoint (Optional): It specifies the string to use for the decimal point.
  • separator (Optional): It specifies the string to use for a thousand separators.

Different use of number_format() function

  1. Basic number format
  2. Decimal number format
  3. Using separator

1. Basic number format

First we use the number_format() function with one argument to format a number with the default thousand separator. Run the following PHP script.

2. Decimal number format

Now, we use the number_format() function with two arguments with the default thousand separator and decimal value

3. Using separator

At last we check passing all parameters in the number_format() function. In the following example , is used for the decimal point and # is used for a thousand separators.

If you want to remove the comma (,) from the number using the number_format() function, you can use the following format.

I hope you find this article helpful.
Thank you for reading. Happy Coding..!! 🙂

If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *