By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,534 Members | 950 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,534 IT Pros & Developers. It's quick & easy.

math.round doesn't work

P: n/a
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.

Nov 2 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Is this a bug?

As far as I'm concerned, yes. Check out this similar thread to see an
explaination:

http://groups.google.com/group/micro...748c717a6c3a36

Thanks,

Seth Rowe
Altman wrote:
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.
Nov 2 '06 #2

P: n/a
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?

rowe_newsgroups wrote:
Is this a bug?

As far as I'm concerned, yes. Check out this similar thread to see an
explaination:

http://groups.google.com/group/micro...748c717a6c3a36

Thanks,

Seth Rowe
Altman wrote:
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.
Nov 2 '06 #3

P: n/a
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?

rowe_newsgroups wrote:
Is this a bug?

As far as I'm concerned, yes. Check out this similar thread to see an
explaination:

http://groups.google.com/group/micro...748c717a6c3a36

Thanks,

Seth Rowe
Altman wrote:
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.
Nov 2 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.