1

When I am trying to multiply 3 (Integer type) with 112.1 (Double) then the result I get contains many decimal points.

Integer a=3;
Double b=112.1;
 Double result=a*b = 336.29999999999995 

But when we multiply 2 (Integer) with 112.1 (Double) then decimal point is so minimal.

Integer a=2;
Double b=112.1;
Double result=a*b = 224.2

Can anybody give me the reason why this strange behavior comes?

sanjeevjha
  • 1,439
  • 14
  • 18

1 Answers1

0

You can't store actual decimal representation of fractions.

This is because computer use binary to save any value, including fractional values.

Therefore, there is some difference between actually saved one and what you want to store.