Chapter 6. Some Useful PHP Built-In Functions

Format a Number

You can specify the number of decimal points that a number displays, a character to separate the number into thousands, and a character to separate the decimal places from the whole number with the number_format() function, as follows:

$f_number = number_format($number,
n_dec,"dec_char","thous_char");

You can use one, two, or four of the parameters, but not three. The default characters are a decimal point (.) to separate the decimal places and a comma (,) to separate the thousands, such as 1,234.56. If you specify only the first two parameters, the default characters are used.

If you specify a number of decimal places smaller than the number of decimal places in the number, number_format ...

Get PHP & MySQL®: Your visual blueprint™ for creating dynamic, database-driven Web sites now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.