|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Rounding issueI am having a rounding problem all over the place and am struggling to
fix it. My problem is that I have an unit price and a quantity. When I multiply them together they come out to 3.705. I want to round this to 2 decimals. It always rounds to 3.70 though. Even when I put math.round(3.705,2) in the debug watch, I get 3.70. How can I get this to round correctly? I know one option would probably be to add .00001 to the value before rounding, as I am sure this is floating point issue, but that might cause a problem in the future with other data. Math.Round(3.705, 2, MidpointRounding.AwayFromZero)
Won't that work? Thanks, Seth Rowe Altman wrote: Show quoteHide quote > I am having a rounding problem all over the place and am struggling to > fix it. My problem is that I have an unit price and a quantity. When > I multiply them together they come out to 3.705. I want to round this > to 2 decimals. It always rounds to 3.70 though. Even when I put > math.round(3.705,2) in the debug watch, I get 3.70. How can I get this > to round correctly? I know one option would probably be to add .00001 > to the value before rounding, as I am sure this is floating point > issue, but that might cause a problem in the future with other data. I was unaware of the third parameter in the round function. It seems
to me that "AwayFromZero" should be the default setting but it obviously is not that way. rowe_newsgroups wrote: Show quoteHide quote > Math.Round(3.705, 2, MidpointRounding.AwayFromZero) > > Won't that work? > > Thanks, > > Seth Rowe > > > Altman wrote: > > I am having a rounding problem all over the place and am struggling to > > fix it. My problem is that I have an unit price and a quantity. When > > I multiply them together they come out to 3.705. I want to round this > > to 2 decimals. It always rounds to 3.70 though. Even when I put > > math.round(3.705,2) in the debug watch, I get 3.70. How can I get this > > to round correctly? I know one option would probably be to add .00001 > > to the value before rounding, as I am sure this is floating point > > issue, but that might cause a problem in the future with other data. Take a look at this link i think it will help you.
http://www.developerfusion.co.uk/show/4252/ Miro Show quoteHide quote "Altman" <balt***@easy-automation.com> wrote in message news:1161183990.987333.237570@m7g2000cwm.googlegroups.com... >I am having a rounding problem all over the place and am struggling to > fix it. My problem is that I have an unit price and a quantity. When > I multiply them together they come out to 3.705. I want to round this > to 2 decimals. It always rounds to 3.70 though. Even when I put > math.round(3.705,2) in the debug watch, I get 3.70. How can I get this > to round correctly? I know one option would probably be to add .00001 > to the value before rounding, as I am sure this is floating point > issue, but that might cause a problem in the future with other data. > Altman,
Maybe not so nice as program but it is very easy to do dim a as double = 3.705 a + .005 dim b as double = Cdbl(a.ToString("d")) I am not always sure from that "d" but if that is wrong have than a look at the overloaded ToString for the correct patern. (In net is used the ISO banking rounding, I never heard who it was using outside that world and they use if definitley not here for their clients). I hope this helps, Cor Show quoteHide quote "Altman" <balt***@easy-automation.com> schreef in bericht news:1161183990.987333.237570@m7g2000cwm.googlegroups.com... >I am having a rounding problem all over the place and am struggling to > fix it. My problem is that I have an unit price and a quantity. When > I multiply them together they come out to 3.705. I want to round this > to 2 decimals. It always rounds to 3.70 though. Even when I put > math.round(3.705,2) in the debug watch, I get 3.70. How can I get this > to round correctly? I know one option would probably be to add .00001 > to the value before rounding, as I am sure this is floating point > issue, but that might cause a problem in the future with other data. >
VB 2005 Form Controls Disappeared!!
Need a tip: How do you streamwrite from two different db tables? UTF-8 encoding problem On Error Goto Next Loop Read another applications textboxes (VB 2005) using a triple-nested for...next loop How to simulate Application.DoEvents in a DLL Dispose problem/crash with maximized MDI child windows need help!! ListView Happiness |
|||||||||||||||||||||||