Rounding...
BlitzMax Forums/BlitzMax Beginners Area/Rounding...
| ||
Hey, I know you can use Floor and Ceil to round to the nearest int.. But what about rounding to the nearest onehundredth? How would you do this? |
| ||
Be aware that floor and ceil don't round the same way as you might think they do. (They are in 'reverse' for negative numbers, floor -6.5 is -7) Times the number by 100, chop off the decimals, then divide it back by 100? |
| ||
How do I 'chop off the decimals' ? |
| ||
Turning it into an integer chops off any decimal part: Local a:Float=1223234.23423 a=a*100 Local b:Int=Int(a) b=b/100 |
| ||
That won't work. B is an integer, so you'll wipe out all the decimal places, and you're doing integer division as well which will do the same no matter where you put the result. You'll need to remove some digits from the start value of "a" too, because there are too many to be stored in a single precision floating point variable. Local a:Float=1.23456 a=a*100 Local b:Int=Int(a) Local c:Float=b/100.0 Print C Of course it won't be perfect because once it's back to a floating point number, you get floating point imprecision. |
| ||
Just locate the "." in a string Use this : SuperStrict Local a:Double = 789.8235446456464 Local decimals:Int = 5 'number of decimals behind the dot Print (Mid (String(a),0,String(a).find(".") + 2 + decimals)) |
| ||
a = Ceil(a*100!)/100 If you only need 100ths, you could also stick with Ints and have the unit be a hundredth of the original. Eg. if you are using pixels, only divide by 100 before using the number for drawing. Be aware that floor and ceil don't round the same way as you might think they do. (They are in 'reverse' for negative numbers, floor -6.5 is -7) Fortunately they work as they should. Unlike in some MS products... |
| ||
Basically there is no way to force all numbers to be to 2 decimal places. The float or double specification may not allow the kind of precision that you'd need to perfectly represent every number from 1.00 to 1.99 - it might only be able to store 1.74 as 1.739923949 or something. If you MUST have 2 decimal places exactly then you will have to use fixed point integers. |