Better Int()?
BlitzPlus Forums/BlitzPlus Programming/Better Int()?
| ||
OK Int rounds numbers that are exactly halfway e.g. 0.5, 1.5 etc towards nearest even number e.g. 0.5 = 0 but 1.5 = 2. I think this is called bankers rounding (for a more fair rounding of money), but it stinks for me! Anyone know a quick way to always round 0.5 down? e.g 1.5 = 1 but 1.6 = 2 |
| ||
I'm not sure of any "Quick way" but this is what I came up with, adjust as needed.Function MyRounding%(Value#) Places#=(Value-Floor(Value)) If Places>0.5 Return Ceil(Value) Else Return Floor(Value) EndIf End Function |
| ||
Function Round(flot#) Return Floor(flot+0.5) End Function |
| ||
lol, nice one. Just me over doing it as usual. |
| ||
Owned. |
| ||
Nice one guys! I was just too tired to work it out and it was causing me a massive bug in a level editor. |
| ||
Too bad this function doesn't just return the integer part, like most other basic's do.... Russell |
| ||
Russell: luckily Floor does that. |
| ||
Not by itself. Floor just rounds 'down' toward negative infinity. Floor(-1.7) returns -2.0 for example. Russell |
| ||
Under other basics, Int(-1.7) would return -1. For the fractional portion, you can use Frac(-1.7), if available or just subtract the Int() portion from the original. Of course, that can be a bit sketchy given the nature of floats (-1.7 could be defined internally as -1.700099 or similar). Beaker's example does help, though. Russell |
| ||
Russell: I knew that, glad u did too! |
| ||
Too bad this function doesn't just return the integer part, like most other basic's do.... Function ClassicInt(flt#) If flt#< 0 Return Ceil(flt#) Else Return Floor(flt#) EndIf End Function |