DecimalFormat and Double.valueOf()
Posted
by folone
on Stack Overflow
See other posts from Stack Overflow
or by folone
Published on 2010-04-22T13:09:30Z
Indexed on
2010/04/22
13:13 UTC
Read the original article
Hit count: 239
Hello. I'm trying to get rid of unnecessary symbols after decimal seperator of my double value. I'm doing it this way:
DecimalFormat format = new DecimalFormat("#.#####");
value = Double.valueOf(format.format(41251.50000000012343));
But when I run this code, it throws:
java.lang.NumberFormatException: For input string: "41251,5"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1224)
at java.lang.Double.valueOf(Double.java:447)
at ...
As I see, Double.valueOf()
works great with strings like "11.1"
, but it chokes on strings like "11,1"
. How do I work around this? Is there a more elegant way then something like
Double.valueOf(format.format(41251.50000000012343).replaceAll(",", "."));
Is there a way to override the default decimal separator value of DecimalFormat
class?
© Stack Overflow or respective owner