Converting string to double with dot [duplicate] - java

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

Related

Float data loss and precision issue [duplicate]

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

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

Properly converting a double variable to string [duplicate]

This question already has answers here:
Converting double to string
(17 answers)
How do I print a double value without scientific notation using Java?
(18 answers)
Closed 6 years ago.
I am trying to take a Double variable called 'startCheckNumber' which should have a value of '40305555' and convert to String. In doing a debug of my code the startCheckNumber shows a value of 4.030555E7. If I do the following command to convert to String it shows it like that instead of '4030555'
String displayCheckNumber = String.valueOf(startCheckNumber) ;
Is there a better way to convert a double variable to String in this case than using ValueOf? I tried 'Double.toString(number)' format and that didn't work right
Thanks
Sorry I should have looked harder. I found and this seemed to work
DecimalFormat decimalFormat = new DecimalFormat("#");
String displayCheckNumber = decimalFormat.format(startCheckNumber);

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.

Java Deduction gives wrong result [duplicate]

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;

Categories

Resources