math.round doesn't work

A

Altman

OK I was having rounding problems before and I didn't realize that
their was a third parameter in the round function that would tell it if
it a 5 to round up. I thought adding this would fix the problem but it
didn't. I have VS 2005 and I am using vb.net try rounding the number
4.935 to 2 decimals:

What I am seeing is the following:
Math.Round(4.935, 2, MidpointRounding.AwayFromZero) 'Result is 4.93
Math.Round(4.935, 2, MidpointRounding.ToEven) 'Result is
4.93

Is this a bug? Am I just missing something?
I want 5 to round up which means AwayFromZero should do it right? In
this case even if I did ToEven it should go to 4.94 but it isn't. Is
it best to just abandon the round function in .Net and make my own. I
feel that rounding is such a basic function that I shouldn't have to
make my own function. Is this a floating point problem with .Net?
Right now I don't know if I can trust the round function that is built
in.
 
A

Altman

So it seems that this problem happens when you use the type double.
Should I just use the decimal type all the time? Is there a benefit of
using decimal over double or vice versa?
 
A

Altman

I notice that if I do the following:
Math.Round(ctype(4.935, Decimal), 2, MidpointRounding.AwayFromZero)

I get the desired result. Is there any problem converting a double to
a decimal like this?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top