1. Computing & Technology

Discuss in my forum

Rounding and Formatting Numbers

By , About.com Guide

4 of 4

Number_format () - Formatting your Numbers
You can use the PHP function number_format to format a number in several ways, including choosing how many decimal points it will have, and choosing the 1000s, and decimal point dividers. It is phrased as number_format ( your_number , optional_decimals , optional_decimal_point, optional_1000_separator ) You can not specify just the decimal point or 1000 separator, if you specify one you need to specify both. When working with decimals, it will function like round () and put things at or over .5 up, and under .5 down.
 <?php
 number_format (1234.567);
 //Returns the number 1,235 

number_format (1234.567, 2); //Returns the number 1,234.57

number_format (1234.567, 2, ',', ' '); //Returns the number 1 234,57

number_format (1234.567, 1, 'a', 'b'); //Returns the number 1b234a6

?>

©2012 About.com. All rights reserved.

A part of The New York Times Company.