I want to get the result 0.054563
from a String
and parse it as a double
. My current attempt looks like,
String number1 = "54,563";
String number2 = "54,563";
String value = "0.0" + number1;
String value2 = "0.0" + number2;
Double rd1 = Double.parseDouble(value.replaceAll(",","."));
Double rd3 = Double.parseDouble(value2.replaceAll(",","."));
System.out.println(rd1);
However, when I run it, I get the following error:
Exception in thread "main" java.lang.NumberFormatException: multiple points
You could use a regular expression to remove all non-digits. The pattern
\D
matches non-digits. Using aPattern
is faster if you need to do this multiple times. So you could do something like,or if you only need to do it once, you could do it inline like
Both of which output your desired