number_format() is one of the most popular PHP function which is used to format numbers with grouped thousands.
Syntax : number_format(number, decimal_values, decimal_point, thousand_separator)
number : The number which has to be formatted.
decimal_values : Specifies number of decimal values after decimal_point.
decimal_point : Specifies the string used as decimal point separator.
thousand_separator : Specifies the string used as thousand separator.
Note :
- The function accepts one, two or four arguments(does not accept three arguments).
- If only first argument is passed, the number is formatted without decimal values and (,) used as thousand separator.
- With two arguments, the number is formatted upto decimal_values passed as argument with (.) as decimal point and (,) as thousand separator.
- With all arguments, the number is formatted upto decimal_values passed as argument, separator for decimal point will be passed as third(decimal_point) argument and fourth(thousand_separator) argument is used as thousand separator.
Examples :
a. Number Formatting with one parameter
<?php
$number = 1258463.254;
echo number_format( $number );
?>
Output : 1,258,463
b. Format number with decimal values
<?php
$number = 1258463.254;
echo number_format( $number, 2);
?>
Output :1,258,463.25
c. Format number with all arguments
<?php
$number = 1258463.254;
echo number_format( $number, 2, '/', '-');
?>
Output : 1-258-463/25
0 Comment(s)