Clare Barrington
Clare Barrington

Reputation: 1155

Math.Round() Decimal Incorrect value

I have goggled and tried a-lot of different things none work. They are provide the same incorrect value.

WRONG

RIGHT

Code to work out VAT

            **VAT = 1.2**

            var totalVat = orderLine.MixPrice - (orderLine.MixPrice / orderLine.VatPerItem);
            var priceMinusVat = orderLine.MixPrice - totalVat;

What I have tried

var roundPriceMinusVat = Math.Round(Convert.ToDecimal(priceMinusVat), 2);
var roundTotalVat = Math.Round(Convert.ToDecimal(totalVat), 2);

var roundPriceMinusVat = Math.Round(Convert.ToDecimal(priceMinusVat), 3);
var roundTotalVat = Math.Round(Convert.ToDecimal(totalVat), 3);

var roundPriceMinusVat = Math.Round(Convert.ToDecimal(priceMinusVat), 3, MidpointRounding.AwayFromZero);
var roundTotalVat = Math.Round(Convert.ToDecimal(totalVat), 3, MidpointRounding.AwayFromZero);

I know technically it is right however as this is customer facing I know someone will work it out with a calculator and email saying "this is wrong it doesn't add up".

I don't really know what to do?

Any help is appreciated.

Thanks,

Clare

Upvotes: 0

Views: 111

Answers (1)

Lutz Lehmann
Lutz Lehmann

Reputation: 25992

Your problem is that the true values are 9.99/1.2=8.325 and 9.99-(9.99/1.2)=1.665. Rounding both up will result in a surplus of 0.005+0.005=0.01. Compute

Compute the tax first, decide on rounding up or down and round the tax, then subtract from price with tax.

Upvotes: 2

Related Questions