vldmrrdjcc
vldmrrdjcc

Reputation: 2132

Rounding of decimal in c# seems wrong

This part of code should speak for itself, my question is can I get 1242.08 result in res variable, and not 1242.07, because in math

b = 1/(1/b)

and in double which has less precision it seems I got good result

Can I fix decimal part of calculation to give me mathematically right result:

decimal rate = 124.2075M;
decimal amount = 10M;
decimal control = decimal.Round(rate * amount, 2); //it is 1242.08

//but
decimal res = decimal.Round(amount * (1 / (1 / rate)), 2);//1242.07 - should be 1242.08
decimal res1 = decimal.Round(amount * 124.2075M, 2);//1242.08 OK

/////////////////////////////////////////////////////////////

//while with double seems OK
double ratef = 124.2075;
double amountf = 10;
double resf = Math.Round(amountf * (1 / (1 / ratef)), 2);//1242.08 OK
double res1f = Math.Round(amountf * 124.2075, 2);//1242.08  OK

Upvotes: 1

Views: 2133

Answers (1)

fubo
fubo

Reputation: 46005

That's a limitation of the datatype decimal that can hold up to 29 digits

The result of the first calculation (res1) does not fit into decimal so you get a invalid/imprecisely result.

decimal rate = 124.2075M;
decimal amount = 10M;

decimal res1 = (1 / rate); //0.0080510436165287925447336111M <- not enough decimal places
decimal res2 = (1 / res1); //124.20749999999999999999999991M
decimal res3 = amount * res2; //1242.0749999999999999999999991M
decimal res4 = decimal.Round(res3, 2); //1242.07M <- correct rounding

Upvotes: 5

Related Questions