Monday, 29 September 2014

number_format in PHP

PHP number_format() function is utilized to format a number with grouped thousands.
PHP number_format() function accepts either one, two, or four parameters (not three).
Assuming that one and only one parameter is given, number will be formatted without decimals, yet with a comma (",") between each group of thousands.
Assuming that two parameters are given, number will be formatted with decimals with a dot (".") in front, and a comma (",") between each group of thousands.
Assuming that every one of the four parameters are given, number will be formatted with decimals, dec_point in place of a dot (".") before the decimals and thousands_sep rather than a comma (",") between each assembly of thousands.

Syntax:
Parameters Description:
number_format(number,decimals,decimalpoint,separator)
number : Required. The number being formatted.
decimals : Optional. Defines the number of decimal points.
decimalpoint : Optional. Defines the separator for the decimal point.
separator : Optional. Defines the thousands separator.
NOTE : If this (separator) parameter is given, all other parameters are needed too.


Example:
<?php
$number = 900000;
echo number_format($number)."<br />";
echo number_format($number,2)."<br />";
echo number_format($number,3)."<br />";
echo number_format($number,2, ',', '.')."<br />";
?>

Output will be:
900,000
900,000.00
900,000.000
900.000,00

0 comments:

Post a Comment