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

Rounding and Formatting Numbers

By Angela Bradley, About.com

1 of 4

Round () - Rounding Numbers

The PHP function round () is used to round numbers up or down based on their value (Any decimal .5 or above is rounded up, anything under .5 is rounded down.) You can specify how many decimals you would like the number rounded to. It is phrased as round ( number, optional_decimals ) If left unspecified, by default it will round to the nearest whole number. You can use negative numbers when specifying the optional_decimals, for example -1 would round to the nearest 10, -2 would round to the nearest 100, etc.
<?php
echo round(3.14159265);
// Value would be 3

echo round(3.9);
// Value would be 4

echo round(3.14159265, 3);
// Value would be 3.142

echo round(314159, -100);
// Value would be 314100
?>

1 of 4

Explore PHP / MySQL

More from About.com

  1. Home
  2. Computing & Technology
  3. PHP / MySQL
  4. Learn PHP
  5. Round PHP Function - PHP Round number - Round number function

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

All rights reserved.