Forum Moderators: coopster

Message Too Old, No Replies

Getting mad with round()

         

tomda

9:04 am on Aug 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am really getting mad with the round function and google search do not help.

I want to round to a superior number only when there are decimal, that is

0.9 return 1
1 return 1
1.1 return 2

Thanks

Netizen

9:50 am on Aug 19, 2004 (gmt 0)

10+ Year Member



What you want is ceil [php.net].

tomda

1:16 pm on Aug 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey hey,
Again a php function that I was not aware of - I will need go through my php cookbook again.

Thanks

httpwebwitch

4:08 pm on Aug 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



those are good functions to know, and easy to remember.

ceiling. goes up. ceil($x)
floor. goes down. floor($x)
round. goes to the nearest integer. round($x)

Did you know:
In my version of PHP,
round(0.5)=1
round(1.5)=2
round(2.5)=3
round(3.5)=4
round(4.5)=5
(a quick test confirms this)

But in an ideal mathematical world,
round(0.5)=0,
round(1.5)=2,
round(2.5)=2,
round(3.5)=4,
round(4.5)=4.

A good rounding of a half should go to the nearest even number. That way, on average half of the "halves" go up, and half go down. That way statistical averages aren't badly skewed by rounding. PHP 4.3.6 doesn't do that, even though the online PHP manual shows a user comment contradicting this.