-2

In My Java code I'm trying to do following

    double a=1769.58;
    double b=986.58;
    double c=a-b;
    System.out.println("Result "+c);

This is retuning the result as 782.9999999999999. but it should be 783.00 what is wrong with this.how can I get correct value and what is the reason for this?

user1688181
  • 496
  • 1
  • 10
  • 27

2 Answers2

0

It's because computer can't stored exactly the good value of floating point. See this response to learn more about this problem.

Community
  • 1
  • 1
JumpIfBelow
  • 53
  • 2
  • 11
0

try this:

double a=1769.58;
double b=986.58;
int c=(int)a-(int)b;