Rounding issue

A

Altman

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.
 
R

rowe_newsgroups

Math.Round(3.705, 2, MidpointRounding.AwayFromZero)

Won't that work?

Thanks,

Seth Rowe
 
C

Cor Ligthert [MVP]

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
 
A

Altman

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.
 

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

Top