I know about the Math.Round and Math.Ceiling methods, but they return a Double and a Decimal. Does VB.NET have any built-in functions which always round a floating point number up, not down, with a return type of Integer? I know there's CInt, but this can round down if it's below 6.5.
1 Answer
From the comments I understood you wanted to round 6.1
to 7
.
Just add 1 and truncate.
If it looks awkward, create a method for it.
Correction:
Unless the number already is truncated.
Addendum:
Note here that doing == with floats is not without problem; you should always have some sort of precision when trying == with floats.
Now when you have decided on this precision - then you can rewrite your code to add 0.999999 (according to precision) and the first add-0.9999-and-truncat works.
Note again that adding 0.9999 is does not really mean you add 0.9999 with our "normal" float.
So if you really want to add 0.9999 you have to work with BND and/or some monetary arithmetics.
Which you "always" should do when calculating money (or any exact decimal stuff)
-
2
-
@LutzL A better approach is to add 0.5 and round, as someone pointed out in the comments. Commented Mar 22, 2014 at 19:54
CInt
on what you get fromRound
orCeiling
. Also, I believeCInt
's rounding behavior is consistent both above and below 6.5 (not that this matters once you've rounded already).6.1
?