1. Home
  2. Computing & Technology
  3. PHP / MySQL

Rounding and Formatting Numbers

By Angela Bradley, About.com

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


?>

Explore PHP / MySQL
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. PHP / MySQL
  4. PHP Functions
  5. Number Format PHP Function - PHP Number_format - format number function

©2009 About.com, a part of The New York Times Company.

All rights reserved.