-1

I there a built-in function for proper mathematical rounding in vb.net?

For example, both functions below return 14.00, but they should return 14.01:

Math.Round(14.004999, 2, MidpointRounding.AwayFromZero)
Decimal.Round(14.004999, 2, MidpointRounding.AwayFromZero)

Are there any other methods that do proper mathematical rounding?

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
ak7483
  • 187
  • 1
  • 14
  • 4
    Why do you think that the answer should ever end with .01? You are rounding to two decimals and the third decimal is 4. – dbasnett Jul 13 '16 at 13:23
  • 1
    If you're expecting it to always round up because of the `AwayFromZero`, note the name of the enum - `MidpointRounding`, it only applies to values that are exactly halfway - rounding otherwise works as usual – James Thorpe Jul 13 '16 at 13:25
  • I'm not expecting to always round up. But I am expecting that the whole number is taken into the account. So in my example the nines would be carried over, so the entire rounding process would go something like this: 14.004999 -> 14.005 -> 14.01 I hope I made myself a bit cleared now :) – ak7483 Jul 13 '16 at 13:33
  • 3
    That's just not how rounding works. `.004999` is less than `0.005` so it will get rounded down. – James Thorpe Jul 13 '16 at 13:42
  • You are right. Guess I've had my head stuck in financial calculations too long and forgot how real math works :D – ak7483 Jul 13 '16 at 13:46

3 Answers3

1

This will produce the desired results, but...

Dim somedata As Double = 14.004999
Dim outd As Double

outd = Math.Round(somedata, 3)
outd = Math.Round(outd, 2, MidpointRounding.AwayFromZero)
Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
dbasnett
  • 11,334
  • 2
  • 25
  • 33
0

Add 1/2 cent to the calculations:

Dim x As Decimal = 14.004999D

Dim y as Decimal = Decimal.Round(x + 0.005D, 2, System.MidpointRounding.AwayFromZero)

Be sure to use the Decimal type and not the Double type in financial math.

Eugene Lisitsky
  • 12,113
  • 5
  • 38
  • 59
BillB4
  • 29
  • 5
-2

What you want is ceiling, not rounding.

Dim d As Decimal = 14.004999
Dim r As Decimal = Math.Ceiling(d * 100D) / 100D
Megas
  • 81
  • 1
  • 9
  • 1
    `"Proper mathematical rounding"` **IS NOT** using ceiling. Ceiling will always round up, while proper mathematical rounding is for example: `0.5 -> 1` and `0.4 -> 0`. _Anything above **and including .5** will be rounded up, while everything **below .5** will be rounded down._ – Visual Vincent Jul 13 '16 at 14:14
  • Correct me if i wrong: you say that 14.004999 should be rounded to 14.01, but .0049 is below .005, thus it rounded to 0 and you receive 14.00, if you want it to be 14.01 you only need to round it up. Where i'm wrong? – Megas Jul 13 '16 at 15:49
  • No I never said that. I said `0.5` should be rounded to `1` and `0.4` to `0`. It was two examples of the type of rounding _that he wants_. However ceiling always rounds up, meaning if his input is dynamic this won't do the trick. – Visual Vincent Jul 13 '16 at 16:35