Basic rounding function

BlitzMax Forums/BlitzMax Beginners Area/Basic rounding function

Mr. Ell'(Posted 2005) [#1]
by some odd chance i ended up needing a rounding function...none existed.

if you ever find yourself in the same situation, heaven knows why...i've done all the thinking for you ;-)



Function round_up:Int(numbertoroundup:Double)
Local i:Int = numbertoroundup
If (i Mod numbertoroundup) <> 0
i:+1
EndIf

Return i
EndFunction

Local x:Double
x = 5.00
Print round_up(x) 'if by some chance you end up passing a value such as 5.00 during discrete operation
x = 5.00001
Print round_up(x) 'returns 6.. oooh la la


~Mr.Ell'


Yan(Posted 2005) [#2]
I'm afraid BMax already has Ceil#() and Floor#() commands for rounding...
Print Int(Ceil(5.00001))
Print Int(Ceil(5.0000))
Print Int(5.00001) ' Same as int(Floor(5.00001))



Mr. Ell'(Posted 2005) [#3]
well there you go folks...that'll teach me to whig out and start programming things at random to meet an impossible deadline!