Home All Groups Group Topic Archive Search About

math.round doesn't work

Author
2 Nov 2006 4:10 PM
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.

Author
2 Nov 2006 4:20 PM
rowe_newsgroups
> 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/microsoft.public.dotnet.languages.vb/browse_thread/thread/c733535c7db0ea4f/8c748c717a6c3a36?lnk=st&q=&rnum=27#8c748c717a6c3a36

Thanks,

Seth Rowe


Altman wrote:
Show quoteHide quote
> 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.
Author
2 Nov 2006 5:35 PM
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?

rowe_newsgroups wrote:
Show quoteHide quote
> > 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/microsoft.public.dotnet.languages.vb/browse_thread/thread/c733535c7db0ea4f/8c748c717a6c3a36?lnk=st&q=&rnum=27#8c748c717a6c3a36
>
> 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.
Author
2 Nov 2006 5:57 PM
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?

rowe_newsgroups wrote:
Show quoteHide quote
> > 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/microsoft.public.dotnet.languages.vb/browse_thread/thread/c733535c7db0ea4f/8c748c717a6c3a36?lnk=st&q=&rnum=27#8c748c717a6c3a36
>
> 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.