Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert String to Double without losing precision in Java?

Tags:

java

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

like image 396
user1938073 Avatar asked Mar 08 '13 16:03

user1938073


People also ask

Can you change a String to a double in java?

We can convert String to Double object through it's constructor too. Also if we want double primitive type, then we can use doubleValue() method on it. Note that this constructor has been deprecated in Java 9, preferred approach is to use parseDouble() or valueOf() methods.

How precise are doubles in java?

The double data type is a 64-bit double-precision IEEE 754 floating-point number. It means that it gives 15-16 decimal digits precision.

Which function will convert a String into a double-precision quantity?

The atof() function converts a character string to a double-precision floating-point value.

What is the method in converting String to double?

We can convert String to double in java using Double. parseDouble() method.


4 Answers

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.

like image 54
PermGenError Avatar answered Oct 24 '22 00:10

PermGenError


Your problem is not a loss of precision, but the output format of your number and its number of decimals. You can use DecimalFormat to solve your problem.

DecimalFormat formatter = new DecimalFormat("#0.00");
String d = new String("12.00");
Double dble = new Double(d.valueOf(d));
System.out.println(formatter.format(dble));

I will also add that you can use DecimalFormatSymbols to choose which decimal separator to use. For example, a point :

DecimalFormatSymbols separator = new DecimalFormatSymbols();
separator.setDecimalSeparator('.');

Then, while declaring your DecimalFormat :

DecimalFormat formatter = new DecimalFormat("#0.00", separator);
like image 40
Rob Avatar answered Oct 24 '22 00:10

Rob


You have not lost any precision, 12.0 is exactly equal to 12.00. If you want to display or print it with 2 decimal places, use java.text.DecimalFormat

like image 33
jalynn2 Avatar answered Oct 24 '22 00:10

jalynn2


If you want to format output, use PrintStream#format(...):

System.out.format("%.2f%n", dble);

There %.2f - two places after decimal point and %n - newline character.

UPDATE:

If you don't want to use PrintStream#format(...), use DecimalFormat#format(...).

like image 22
bsiamionau Avatar answered Oct 24 '22 00:10

bsiamionau