Java Deduction gives wrong result [duplicate] - java

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 8 years ago.
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?

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

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

Related

Java : confusing division result with 2 doubles [duplicate]

This question already has answers here:
Why does floating-point arithmetic not give exact results when adding decimal fractions?
(31 answers)
Retain precision with double in Java
(24 answers)
Closed 5 years ago.
Can anyone explain what's going on here? My calculator insists that the result of equitation is 13201.
double test = 132.01/0.01D;
System.out.println(test); <- 13200.999999999998
Probably, this is very simple question, but I really don't understand. Most of questions in SO on similar topic involves Integer and Double.

java-Not getting the result with decimal places [duplicate]

This question already has answers here:
Why does integer division code give the wrong answer? [duplicate]
(4 answers)
Closed 6 years ago.
I am doing simple calculation in java. Expected result is 51.3348 but what I am getting is 51.0, here is my calculation
float percent = (7819140000l-3805200000l)*100/7819140000l;
Is that problem with datatype? How can I resolve this to get value as 51.3348
Thanks in Advance
add an f to one of the values:
float percent = (7819140000l-3805200000l)*100f/7819140000l;
if yiu do not do it, Java will make a devision by long values

Converting string to double with dot [duplicate]

This question already has answers here:
How do I print a double value without scientific notation using Java?
(18 answers)
Closed 6 years ago.
I need to convert string value to double with dot. Here is simple code
double dValue=Double.parseDouble("999999999.99");
System.out.println(dValue);
output is: 9.9999999999E8
When i gave value like 10000 or 100000 it works. Help me to overcome this problem.
You could use BigDecimal and toPlainString() for that.
BigDecimal dValue= new BigDecimal("999999999.99");
System.out.println(dValue.toPlainString());
Output:
999999999.99
You can use String.format
System.out.println(String.format("%.2f", dValue));

Not getting correct answers for sum of two double value [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Why do I see a double variable initialized to some value like 21.4 as 21.399999618530273?
(14 answers)
Closed 7 years ago.
I have below program:
double tax = 180.09;
double gTotal = 277.08;
double total = tax + gTotal;
System.out.println(total);
After execution it outputs 457.16999999999996.
After Google search I found that one of the cause for this behavior is that there may be no precise binary representation of the fraction part.
To verify that I tried below:
double dNum = 457.17;
System.out.println(dNum);
It gave correct output as 457.17. In this case, why it is not printing 457.16999999999996.

Wrong double calculation [duplicate]

This question already has answers here:
How can I handle precision error with float in Java?
(9 answers)
Closed 8 years ago.
some calculations with doubles return the wrong result.
E.g.
System.out.println(""+(0.05+0.01));
output
0.060000000000000005
What can I do to correct this error?
Doubles are not made for precise calculations (see Round to 2 decimal places) - for precise calculations, use BigDecimal instead.

Categories

Resources