Search code examples
.netvb.netrounding-errorrounding

Dividing a 2 digit decimal point with out rounding off the results


im having a problem on the results of the program that i'm developing.

Like for example if im having a number of 8672.59 and i would like to divide it into 2 the result should be 4336.295. The question is how can i convert it to a 2 digit decimal point both of them with rounding off the results so that i could still get the correct amount when i add the 2 numbers..

Example.

8672.59 / 2 = 4336.295

if rounded off to 2 digit digimal point the result is 4336.30

how can i convert it to 2 digit decimal point with out rounding off so that if it is multiplied by two the result would be the same as 8672.59 not 8672.60

Thank You!


Solution

  • Subtract the rounded result from the whole.

    Dim Total as Double = 8672.59
    Dim OneHalf = Math.Round(Total/2, 2)
    Dim SecondHalf = Total - OneHalf
    

    In your example, Total = 8672.59, so OneHalf = 4336.30 (rounded)

    SecondHalf = 8672.59 - 4336.30 = 4336.29.