Double.parseDouble(String) : Problem to parse large String

Hello,

I need to convert A String to a Double in my application without rounding of digits.

I have used the Double.parseDouble(String) method.

The maximum string length can be 17 including dot(.)

So if my String is (of length 17)

Eg.

S= ?999999999.999999?then I am getting the double value as

d= 9999999999.999998(compare the last digit)

If s = ?999999999.111111?then d = 9999999999.111110

If s = ?999999999.555555?then d = 9999999999.555555 (result that I want)

If s = ?999999999.666666?then d = 9999999999.666666 (result that I want)

If s = ?999999999.777777?then d = 9999999999.777777 (result that I want)

If s = ?999999999.888888?then d = 9999999999.888887

If s = ?999999999.999999?then d = 9999999999.999998

If s = ?123456789.123456?then d = 9123456789.123456

But string length up to 16 is giving me the accurate result

So any body can explain me that why it is happening? And how can i get the accurate result.

Thanks in advanced.

[1104 byte] By [jLittleLearnera] at [2007-11-27 9:45:51]
# 1
Try java.math.BigDecimal instead.
prometheuzza at 2007-7-12 23:55:17 > top of Java-index,Java Essentials,Java Programming...
# 2
> Try java.math.BigDecimal instead.Indeed. Just pass this big string into the constructor. Best way to use BigDecimal anyway
georgemca at 2007-7-12 23:55:17 > top of Java-index,Java Essentials,Java Programming...
# 3
Hi,Thanks for your replyI had used BigDecimal bd = new BigDecimal(str);double d = bd.doubleValue();In is case i am also getting the same result.
jLittleLearnera at 2007-7-12 23:55:17 > top of Java-index,Java Essentials,Java Programming...
# 4
Yes, keep it as BigDecimal, don't convert it to double.As doubles have limited precision, you can't represent all values exactly.
-Kayaman-a at 2007-7-12 23:55:17 > top of Java-index,Java Essentials,Java Programming...
# 5
Hi,Thanks for your reply.After parsing the String to double , i have to store the data in database. and in database the column datatype is DECIMAL and my databse is DB2. So if i don't convert in double then how will i store it?
jLittleLearnera at 2007-7-12 23:55:17 > top of Java-index,Java Essentials,Java Programming...
# 6

> After parsing the String to double , i have to store

> the data in database. and in database the column

> datatype is DECIMAL and my databse is DB2. So if i

> don't convert in double then how will i store it?

With the help of the PreparedStatement.setBigDecimal() method, I guess.

TimTheEnchantora at 2007-7-12 23:55:17 > top of Java-index,Java Essentials,Java Programming...