Archive: round(n)


26th October 2004 02:02 UTC

round(n)
Would people find it useful is there was a round(,) function. Eg. round1(x1) would round x1 to the nearest whole number and round10(x1) would round x1 to the nearest 10. It would be useful to use with the boolean functions.


26th October 2004 13:19 UTC

Don't you *nearly* have thsi already

YOu have the int function right that rounds towards zero

e.g.

x = 3.1; int(x) = int(3.1) = 3
x = 3.6; int(x) = int(3.6) = 3;

Not rounding just truncating

now if you do

int(x+0.5) it is (for positive numbers) the same as your round(x)

e.g. in the above example again

x = 3.1; int(x+0.5) = int(3.1+0.5) = int(3.6) = 3
x = 3.6; int(x+0.5) = int(3.6+0.4) = int(4.1) = 4

and that is the same as round is it not.

It gets a little more complex for doing negative numbers as well in the same equation but can be done with the odd if.

edit/ and round10 is not there far off either

10*int((x/10)+0.5) or probably sightly quicker 10*int((x*0.1)+0.5)

but you can have any rounding equation.

This is all from memory and no way to check as no winamp here atm but hopefully the principles are there.


26th October 2004 19:43 UTC

ceil(bla) rounds up, floor(bla) rounds down.
bla|0 rounds to the nearest integer

ceil(3.2356)=4
floor(6.9123)=6
3.42135|0=3
3.5555|0=4

If you want it to round to decimals you have to multiply the number by power of 10 before you round it (10 for 1 decimal, 100 for 2 decimals etc.), and then divide it by the same number after you round it

ceil(3.2356*10)/10=3.3
floor(6.9123*100)/100=6.91
((3.42135*1000)|0)/1000=3.421


27th October 2004 00:31 UTC

Sweet, thanks.


27th October 2004 13:22 UTC

sorry didn't realise you had them functions although I didn't think of |