1. Computing & Technology

Discuss in my forum

Rounding and Formatting Numbers

By , About.com Guide

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 ?>

©2012 About.com. All rights reserved.

A part of The New York Times Company.