# How to covert a String value to double without loosing the precision

Greenhorn
Posts: 5
Hi,

How can I convert a String value "106.4000" to a double, without loosing the precisions after 4 ?

If I use,

double dbl = Double.parseDouble("106.4000");

I am getting 106.4 only.

But I need the exact string value as double.

regards,

Peter Chase
Ranch Hand
Posts: 1970

A "double" value is stored in binary. The idea of trailing decimal zeros, as in 106.4000, is meaningless in a "double" value.

A "double" value is also floating-point. This means it cannot store all decimal values (whole or fractions) with perfect accuracy. If you don't know about this stuff, Google for "floating point".

Campbell Ritchie
Sheriff
Posts: 50171
79
Welcome to JavaRanch

Your problem sounds like a job for . . . BigDecimal!

Santhosh Kumar
Ranch Hand
Posts: 242
Mathematically,

So you cannot keep the preceding zeros before the decimal and trailing zeros after the decimal.

Campbell Ritchie
Sheriff
Posts: 50171
79
Try this, and see what BigDecimal makes of leading and trailing zeroes:

Campbell Ritchie
Sheriff
Posts: 50171
79
Originally posted by Santhosh Kumar:
Mathematically,

So you cannot keep the preceding zeros before the decimal and trailing zeros after the decimal.
Actually, I agree with the behaviour of BigDecimal that 1.1 is not exactly equivalent to 1.1000. Change the last statement of my BigDecimalTest class which I posted a few minutes back to read like this, and try again.

Ilja Preuss
author
Sheriff
Posts: 14112
Originally posted by Campbell Ritchie:
Actually, I agree with the behaviour of BigDecimal that 1.1 is not exactly equivalent to 1.1000.

But mathematically, it is, isn't it?

Campbell Ritchie
Sheriff
Posts: 50171
79
We're not here to do maths . . . BigDecimal does exactly what the original poster wants, retain the value and the original precision.

Piet Verdriet
Ranch Hand
Posts: 266
Originally posted by Campbell Ritchie:
We're not here to do maths . . . BigDecimal does exactly what the original poster wants, retain the value and the original precision.

Well, it really has nothing to do with precision but rather with presentation (they're not the same). If the OP wants to have 4 leading numbers then the obvious choice would be to use java.text.DecimalFormat with the pattern: "0.0000":

Ilja Preuss
author
Sheriff
Posts: 14112
Originally posted by Campbell Ritchie:
We're not here to do maths . . .

It sounded to me like you were implying that the behavior of primitive doubles was somehow not fully correct. To that I would answer that it shows exactly the behavior a mathematician would expect.

I agree that if you need different behavior, you should use something different, and if that's BigDecimal in this case, fine.

Campbell Ritchie
Sheriff
Posts: 50171
79
Originally posted by Ilja Preuss:
It sounded to me like you were implying that the behavior of primitive doubles was somehow not fully correct. . .
More likely, expressing myself awkwardly. Sorry. We seem all to agree in the end however.

Ilja Preuss
author
Sheriff
Posts: 14112

Ranch Hand
Posts: 73
Originally posted by Ilja Preuss:

But mathematically, it is, isn't it?

Yes, and no.
1.1 equals 1.100, but 1.100 has better precision than 1.1. This can be illustrated by this example: 1.100 rounded to two digits is 1.1, but so is 1.109, 1.14 etc.
So 1.1 really is only precise to say that it is a number N 1.15<N<=1.05, while with 1.100 is 1.150<N<=1.050.

But of course, this doesn't matter much to a computer.