AppDividend
Latest Code Tutorials

PHP number_format() Function Example | number_format() In PHP

0

PHP number_format() Function Example | number_format() In PHP is today’s topic. The number_format() function formats the number with grouped thousands. PHP number_format() function is the inbuilt function in PHP. The number_format() function accepts either one, two, or four parameters (not three). It returns the formatted number.

PHP number_format() Function

The syntax of number_format() function is following.

number_format(number, decimals, decimalpoint, separator)

The number parameter is required, and the number to be formatted. If there are no other parameters are set, the number will be formatted without the decimals and with the comma (,) as the thousands separator.

The decimals are optional, and it specifies how many decimals.

If this parameter is set, the number will be formatted with the dot (.) notation as the decimal point.

The decimalpoint is optional, and it specifies what string to use for a decimal point.

The separator is optional, and it specifies what string to use for the thousands separator. Only the first character of the separator is used. For instance, “xxx” will give the same output as the “x”. If this parameter is given, all other arguments are must present as well.

See the following code.

<?php
   echo number_format("1000000")."\n";
   echo number_format("1000000",2)."\n";
   echo number_format("1000000",2,",",".");

See the output.

➜  pro php app.php
1,000,000
1,000,000.00
1.000.000,00                                                                    
➜  pro

Let’s see another example.

<?php

  $data = 1111.1;
  $formattedData = number_format($data)."\n";
  echo $formattedData;
  $formattedNum = number_format($data, 2);
  echo $formattedNum;

See the output.

➜  pro php app.php
1,111
1,111.10                                                                        
➜  pro

If you pass anything instead of numbers, it gives warning. See the code.

<?php

$data = "ELEVEN"; 

echo number_format($data)."\n\n"; 

echo number_format($data, 3);

See the following output.

➜  pro php app.php
PHP Warning:  number_format() expects parameter 1 to be float, string given in /Users/krunal/Desktop/code/php/pro/app.php on line 5

Warning: number_format() expects parameter 1 to be float, string given in /Users/krunal/Desktop/code/php/pro/app.php on line 5


PHP Warning:  number_format() expects parameter 1 to be float, string given in /Users/krunal/Desktop/code/php/pro/app.php on line 7

Warning: number_format() expects parameter 1 to be float, string given in /Users/krunal/Desktop/code/php/pro/app.php on line 7
➜  pro

#French notation

See the following code which represents the number in french format.

<?php

$number = 1111.21;

echo number_format($number, 2, ',', ' ');

See the output.

➜  pro php app.php
1 111,21                                                                                                      
➜  pro

Be careful, when you’re using French notation
means : number_format(124.25, 2 , ‘,’ , ‘ ‘) with ‘,’ as dec_point,

Don’t forget to specify thousands_sep that default is ‘,’ to another value; otherwise, the function will return null.

#Transform PHP string to two decimal places

See the following code which transforms PHP String to two decimal places. It removes other digits after two decimal places.

<?php

$number = 1111.1111;

echo number_format((float)$number, 2, '.', '');

See the output.

➜  pro php app.php
1111.11                                                                                                       
➜  pro

Finally, PHP number_format() Function Example | number_format() In PHP is over.

#Recommended Posts

PHP Strtotime Example | PHP Strtotime() Function Tutorial

PHP Strlen Example | strlen() Function Tutorial

PHP strstr() Function Example | strstr() Method In PHP

PHP Substr Example | PHP String substr() Function Tutorial

PHP str_replace Example | PHP str_replace() Function Tutorial

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.