number_format() is intended for bitwise formatting of numbers with separation of the fractional part. The specific format of the received value depends on the number of arguments passed. The function can take one, two or four parameters and returns the processed value as a string.
The first and required argument to the
number_format() function in PHP is the actual number to be formatted. The expression will look like this:
string number_format(float $number)
$number must be a floating point number.
The result of the function will be a string containing the original number, divided by digits. The fractional part will be discarded with rounding. The comma is used as a separator of digits.
In the second example, the fractional part was rounded off.
If you want to preserve the fractional part, you need to pass the second argument to the PHP function
string number_format(float $number, int $decimals)
$decimals is an integer,specifying the number of decimal places to store. By default, it is zero, so when passing one argument, the fractional part is discarded.
After accepting two parameters, the function will return a formatted string, using a dot as the decimal separator.
In this case, the fractional part is also rounded off. Zeros are substituted for missing digits.
Standard delimiters used by the PHP function
number_format() can be changed to any other characters. The third parameter is responsible for the separator of the fractional part, and the fourth - for the interdigit character.
string number_format(float $number, int $decimals, string $dec_point, string $thousands_sep)
$thousands_sep are string data. You can pass any character as a delimiter, as well as a space or an empty string. From the sequence of characters, only the first one will be used, the rest will be cut off.
The php number_format function works in examples with four parameters:
The separators are replaced by custom ones, all other rules are preserved.
The PHP function
number_format() gives the programmer a lot of freedom in formatting numeric values. To obtain an adequate result, it is important to follow the syntax established by the specification and remember thatthat the return value of the function is of string type.