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.

So please help me to do this.

Thanks in advance...

regards,

Nishad P

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".

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.

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.Originally posted by Santhosh Kumar:

Mathematically,

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

Sheriff

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?

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus

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":

Sheriff

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.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus

Sheriff

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus

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.