What is the optimal way to pass a String
to Double
?
Taking into account if the string is null
is equivalent to 0.0
and if there is no number to convert also 0.0
0.125 > 0.125
a3.72b > 3,72
abcde > 0.0
null > 0.0
I have the following but with a bug in passing a3.72b
that returns 0.0
and I would like it to return 3.72
, not if with a regular expression the whole process is more efficient
private static Double StringToDouble(String s) {
Double d = 0.0;
try {
d = (s!=null) ? Double.valueOf(s) : 0.0;
}catch (NumberFormatException e){
System.out.println("not a number");
}
return d;
}