User Contributed Notes: round
mlina@dds.nl
18-Mar-1999 04:55
Bare in mind that "you can't rely on x.5 being accurate, and thus the behavir of round() can be considered undefined." (see BugDatabase id #984
aulbach@unter.franken.de
13-Aug-1999 12:50
Here is a more detailed description, taken from
http://www.progressive-comp.com/Lists/?l=php3-general&m=93230944323512&w=2
<PRE>
> I tried
> round(11.5) and I get 12
> round(182.5) and I get 182...
>
> You are right.
> It could be a bug in the round() function.
This is normal and reflects how computers store floating point numbers.
11.5 is not actually 11.5. It is 11.49999999999999999 or
11.50000000000000000001. Floating point numbers are approximated using
some level of precision. Think of 1/3. How would you write that?
0.3333333333... when do you stop? How many 3's do you need to accurately
write 1/3? To be completely accurate you need an infinite amount of 3's
and that just isn't feasible, so at some point you just have to say that
you have enough precision.
This means that round(11.5) is not defined. If you want to always round
up on .5, you should add some small (less than your desired precision)
value to the number you feed to round. And if you want to always round
down on .5 you should substract it.
eg.
$fuzz = 0.00000001;
echo round(11.5 + $fuzz);
-Rasmus
</PRE>
jproffitt@sfrep.com
08-Sep-1999 06:09
Most computers use a binary format for representing floating point numbers internally. In binary numbers the fraction 1/2 can easily be expressed exactly: 0.1
What's the problem?
blake@pythoness.com
27-Sep-1999 06:52
Looks like banker's rounding to me. With banker's rounding, you always round to the nearest even number when rounding an x.5. Therefore round(2.5)=2 and round(11.5)=12.
shashin62@hotmail.com
08-Nov-1999 06:36
Is there a way to round off the numbers to 2 digits after the decimal ??
niteraven@eqmaps.com
15-Nov-1999 01:09
The easiest way I could think of to round a number to two digits was this:
round(100*number)/100
tstrum@salter.com
06-Dec-1999 03:40
The following proves this is "banker's" rounding. So - rounding anything that's exactly x.5 will result in a a round number being returned.
<pre>
$i=1;
while ($i<100) {
$halfs = $i/2;
$roundedhalf = round($halfs);
if ($rounded
print ("$halfs rounds to $rounded ");
$i++;
}
</pre>
tstrum@salter.com
06-Dec-1999 03:43
Ooopsy - here't the correct code:
<pre>
$i=1;
while ($i<100) {
$halfs = $i/2;
$roundedhalf = round($halfs);
print ("$halfs rounds to $rounded ");
$i++;
}
ijones@mbcomp.co.uk
09-Feb-2000 06:39
If you want a function that rounds up .5 then here it is.
function rounder($value){
$length=strrpos($value,".");
$number=substr($value,$length+1,$length+2);
$start=substr($value,0,$length);
if ($number>4){return $start+1;}else{return $start;}
}
nickz@vinton.com
16-Mar-2000 07:23
Sure would be nice to have precision on the round function, such as round(string, precision) so that round(32.5555,2) returned 32.56. Or almost as good would be a truncate function (might be easier to add, I don't know) so that truncate(32.5555,2) returned 32.55.
reduz@anime.com.ar
21-Mar-2000 12:30
i think people is messing up too much here..
if you want to round a number some decimals inside.. (example: 35.55555 to 35.56) should be easier:
(if $number value is 35.555555)
$value=round($number*100)/100;
voila!
patrick@e-factory.nl
29-Mar-2000 09:24
Rounding values
f.i. 62.32 --> 62.35
125.15 --> 125.15
36.28 --> 36.30
Next function works for me
function rounder($val)
{
$pos=strrpos($val,".");
$num=substr($val,$pos+2,1);
if ($num=="0")
{
return number_format((round($val*10)/10),2);
}
elseif ($num>4)
{
return number_format((round($val*10)/10),2);
}
else
{
return number_format((round($val*10)/10)+0.05,2);
}
}
Greetings,
Patrick Drummen
bens@saber.net
13-May-2000 12:03
round is kinda pitiful! What if I want to round an integer to 2 decimal places? (eg: 9.898 -> 9.90, or 4.223 -> 4.22 ?)
Tried to get what I wanted by multiplying and dividing, but that sometimes produces errors on long strings.
Now, I'm trying something with arrays. (All this to make 4.563 = 4.56!)
-Ben
twan@ecreation.nl
15-May-2000 06:51
If you'd only want to round for displaying variables (not for calculating on the rounded result) then you should use printf with the float:
printf ("%6.2f",3.39532);
This returns: 3.40 .
hutch@bdel.com
29-Jun-2000 02:51
I just happened to read the fixes documented at the php4 release and found that adding a second argument to the round function like this:
<pre>round(3.01299999,3)</pre> would round to the number of decimal places indicated in the second argument (in the case of the example to 3.013).
humbads@umich.edu
05-Jul-2000 12:26
Another way to print a float to 2 decimal points of precision is to use the number_format() function (instead of printf or dividing/multiplying by 100). For example: <pre>$cost = 2100.5666;</pre> <pre>echo number_format($cost, 2);</pre>--Returns the string "2,100.56".
humbads@umich.edu
05-Jul-2000 12:32
It actually returns "2,100.57", so apparently, the number_format function rounds the numbers too.
ynniv@cc.gatech.edu
28-Jul-2000 05:19
A note on an old thread about rounding... Proper rounding is to round 'even' on .5 (ie 1.5 to 2 and 2.5 to 2). The reason for this is actually scientific (I haven't heard of the banking application, altho it does lend itself as well): when you make calculations you have to keep your significant figures. If your data is accurate to 2 decimal places you must round off to exactly two places. If you always rounded that .5 up (or down), then you round one way 5 out of 9 times and thus change the distribution of random data. The fix is to round up half of the time and down half of the time. This could be accomplished by 'rounding odd', but the standardized way is to always 'round even'. Unfortunately this isn't what they taught us in 1st grade, so most people think that you always round up. If you still want to, you'll have to either add a small fraction and risk inaccurate calculations (1.49 + 0.01 rounds to 2) or call ceil() on your number before rounding.
ynniv@cc.gatech.edu
28-Jul-2000 05:29
Sorry, that was quite an oversight.. you obviously don't want to use ceil() unless you really mean it. The proper fix is to round to one digit more than you need before adding the small fraction: (to get $places decimals) function round_up($x, $places) round($x, $places + 1); $x = $x + 1 / (pow(10, $places + 2)); // one magnitude less than the first round round($x, $places);
return $x;
} (code not tested, YMMV) Thats the general idea. Its a little more code, but if you make it a function you may never even notice.