Float data loss and precision issue [duplicate] - java

This question already has answers here:
Retain precision with double in Java
(24 answers)
Closed 5 years ago.
String abc=11235.271;
Float.parsefloat(abc);
o/p 11235.271 no data loss
while as in
String abc=58996.706;
Float.parsefloat(abc);
o/p 58996.707
.001 is getting added after decimal
Can anyone pls help me on this

consider using a double or bigDecimal:
String abc2 = "58996.706";
System.out.println(Double.parseDouble(abc2));
System.out.println(new BigDecimal(abc2));

Related

How can put what come after the decimal in a integer in java? [duplicate]

This question already has answers here:
Get the decimal part from a double
(18 answers)
Closed 5 months ago.
This post was edited and submitted for review 5 months ago and failed to reopen the post:
Original close reason(s) were not resolved
For example if I had a (double 123.987) how can take what comes after the right of the decimal and make it A integer (987)?
Well you can use String and split to derive your output:
double number = 123.456;
String numberString = String.valueOf(number);
String[] digits2 = numberString.split("\\.");
System.out.println(digits2[1]);
After having this output, you can cast it to integer. Should not be that hard

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));

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