How can I convert String to Double without losing precision in Java?
java
Solution
Use `BigDecimal` Instead of a double:
String d = "12.00"; // No need for `new String("12.00")` here
BigDecimal decimal = new BigDecimal(d);
This works because `BigDecimal` maintains a "precision," and the `BigDecimal(String)` constructor sets that from the number of digits to the right of the `.`, and uses it in `toString`. So if you just dump it out with `System.out.println(decimal);`, it prints out `12.00`.
Problem
Tried as below ``` String d=new String("12.00"); Double dble =new Double(d.valueOf(d)); System.out.println(dble); ``` Output: 12.0 But i want to get 12.00 precision please let me know correct way without using format() method in string class