The Wayback Machine - https://web.archive.org/web/20240623185619/https://www.geeksforgeeks.org/php-number_format-function/
Open In App

PHP number_format() Function

Last Updated : 21 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The number_format() function is an inbuilt function in PHP which is used to format a number with grouped thousands. It returns the formatted number on success otherwise it gives E_WARNING on failure.

Syntax:

string number_format ( $number, $decimals, $decimalpoint, $sep )

Parameters: This function accepts four parameters as mentioned above and described below:

  • $number: It is required parameter which specified the number to be formatted. If no other parameters are set, the number will be formatted without decimals and with the comma (, ) as the thousands separator.
  • $decimals: It is optional parameter and used to specifies decimals. If this parameter is set, the number will be formatted with a dot (.) as the decimal point.
  • $decimalpoint: It is optional parameter and used to specifies the string to use for the decimal point.
  • $sep: It is optional parameter and used to specifies string to use for thousands separator. If this parameter is given, then all other parameters are required.

Return Value: It returns Formatted Number in case success, otherwise it gives E_WARNING in failure.

Examples:

Input: $number = 100000
Output: 10, 000

Input: $number = 10000 
       $decimals = 3 
       $decimalpoints = "." $sep =,
Output: 10, 0000.000 

Below programs illustrate The number_format() function in PHP:

Program 1:




<?php
$num1 = "999999.49";
  
// With out decimal point parameter
echo number_format($num1)."\n";
  
// With decimal Point parameter
echo number_format($num1, 3)."\n";
   
$num2 = "9999999.99";
  
// With out decimal point parameter
// return Round value
echo number_format($num2)."\n";
  
// With decimal Point parameter
echo number_format($num2, 3)."\n"; 
  
// With All four parameters
echo number_format("1000000.99", 3, "#", "GGG");
  
   
?>


Output:

999,999
999,999.490
10,000,000
9,999,999.990
1GGG000GGG000#990

Program 2: If pass anything instead of numbers it gives warning.




<?php
$num = "GFG";
  
// With out decimal point parameter
echo number_format($num)."\n\n";
  
// With decimal Point parameter
echo number_format($num, 3);
   
?>


Output:

PHP Warning:  number_format() expects parameter 1 to be float,
string given in /home/ac476aaecea758334cb8ed146bcbb8f6.php on line 5

PHP Warning:  number_format() expects parameter 1 to be float, 
string given in /home/ac476aaecea758334cb8ed146bcbb8f6.php on line 8

Program 3: This function does not accept three parameters, only accept 1, 2 or 4 parameters.




<?php
$num = 1000000;
  
// passing 3 parameters It gives errors because function
// accepting only 1, 2 or 4 parameters
echo number_format($num, 3, ", ");
?>


Output:

PHP Warning:  Wrong parameter count for number_format() 
in /home/e426108b066d9a86366249bf7b626d19.php on line 6

Reference: http://php.net/manual/en/function.number-format.php



Previous Article
Next Article

Similar Reads

How to get the function name inside a function in PHP ?
To get the function name inside the PHP function we need to use Magic constants(__FUNCTION__). Magic constants: Magic constants are the predefined constants in PHP which is used on the basis of their use. These constants are starts and end with a double underscore (__). These constants are created by various extensions. Syntax: $string = __FUNCTION
1 min read
How the User-Defined Function differs from Built-in Function in PHP ?
In PHP, functions are blocks of reusable code that perform specific tasks. They enhance code readability, modularity, and maintainability by encapsulating logic into named units. PHP functions can be categorized into two main types: user-defined functions and built-in functions. User-Defined FunctionsUser-defined functions are functions created by
2 min read
PHP 5 vs PHP 7
PHP is a server side scripting language designed for web development by Rasmus Lerdorf in 1994. Since its launch in 1994 PHP has become an industry standard supporting almost 80% of the websites ( 79.8% to be precise) with its closest competitor being ASP.Net at 19.8% and others like Ruby, Java trailing far behind. The PHP development team released
4 min read
PHP | Get PHP configuration information using phpinfo()
PHP provides us with a built-in function phpinfo() which gives us the details about the PHP version and PHP configuration of PHP installed in our system. To know about the Configurations and PHP version which is installed in your computer, a simple PHP script can be used. The script consists of a PHP function called "phpinfo()" which outputs inform
2 min read
PHP | php.ini File Configuration
At the time of PHP installation, php.ini is a special file provided as a default configuration file. It's very essential configuration file which controls, what a user can or cannot do with the website. Each time PHP is initialized, the php.ini file is read by the system. Sometimes you need to change the behavior of PHP at runtime, then this config
5 min read
How to import config.php file in a PHP script ?
The include statement in PHP copies the code of text from the file mentioned, into the file that uses the include statement. It directs the preprocessor to insert the content specified into the following program. The name of the file to be included is written in double-quotes. It is a good practice to write the basic database details and user detai
2 min read
How to include content of a PHP file into another PHP file ?
Including the content of a PHP file into another file reduces the complexity of code by reducing the code and improving its modularity by splitting the code into different files so that it is easy to understand and manage. There are two ways to do it by using the following PHP functions. PHP include() Function: It will show the warning if the file
2 min read
What is New in PHP Type Hinting Support in PHP 8?
PHP 8 introduced several enhancements and new features related to type hinting. That will check the code for their respective type so that we can use our code efficiently. These are the following features used in PHP Type Hitting Support: Table of Content Union TypesMixed TypeStatic Return TypeInheritance of Contravariant ParametersInheritance of P
3 min read
PHP | imagecreatetruecolor() Function
The imagecreatetruecolor() function is an inbuilt function in PHP which is used to create a new true-color image. This function returns a blank image of the given size. Syntax: resource imagecreatetruecolor( $width, $height ) Parameters: This function accepts two parameters as mentioned above and described below: $width: This parameter is used to s
2 min read
PHP fpassthru( ) Function
The fpassthru() function in PHP is an inbuilt function which is used to read data from a current position from a specified file until end of file and then write the result to the output buffer. The file which has to be read is sent as a parameter to the fpassthru() function and it returns the number of characters passed on success or FALSE on failu
2 min read
three90RightbarBannerImg