problem with divide operation

  • Thread starter Thread starter Maor Mishkin
  • Start date Start date
M

Maor Mishkin

I have a problem with divide operation,
the example is:
double a =10.0;
double b =220.0;
double divAns = a/b;
the calculation true result is 0.04545454545454545
but for some resone the calculation is changing to get the result
0.0454545468091
if you know how can i get the system to calc the first calculation or if you
know of an object like BigNumber in java that can make
calculation with a specipic precision it could realy help me alot.
Maor
 
Maor,

The reason for this is because of the representation of floating point
numbers. It can not always be exact.

However, you should be able to get a more exact result using the Decimal
structure. It is geared for greater precision when doing floating point
calculations.

Hope this helps.
 
You could use decimal type:

decimal val1 = 10.0m;
decimal val2 = 220.0m;
decimal result = val1/val2;
Console.WriteLine(result);
Console.ReadLine();


This will give the expected result

José
 
Back
Top