0

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.

8
  • 1
    You can always use CInt on what you get from Round or Ceiling. Also, I believe CInt's rounding behavior is consistent both above and below 6.5 (not that this matters once you've rounded already). Commented Mar 21, 2014 at 22:20
  • Just add .5 to it before you round it... Commented Mar 21, 2014 at 22:23
  • Are you saying you want to round up even with 6.1?
    – OneFineDay
    Commented Mar 21, 2014 at 22:24
  • @Asad - So there's no single method that does that?
    – Lou
    Commented Mar 21, 2014 at 22:26
  • 2
    Why not write your own.
    – OneFineDay
    Commented Mar 21, 2014 at 22:26

1 Answer 1

0

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
  • 2
    But what does that to 7.0? Commented Mar 22, 2014 at 8:58
  • @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

Not the answer you're looking for? Browse other questions tagged or ask your own question.