SafeParse { public static Double stringToDouble(String input) { Double result = 0d; input = input.replace(",", "."); try { result = Double.parseDouble(input); } catch (Exception e) { } return result; }  static Double stringToDouble(String input); static Integer stringToInt(String input); static Long stringToLong(String input); static Float stringToFloat(String input);  }
@Test public void stringToDoubleTest() { assertThat(0.1234, is(SafeParse.stringToDouble("0.1234"))); assertThat(0.1234, is(SafeParse.stringToDouble("0,1234"))); assertThat(0.5436564812, is(SafeParse.stringToDouble(".5436564812"))); assertThat(0.5436564812, is(SafeParse.stringToDouble(",5436564812"))); assertThat(1000500100900.0, is(SafeParse.stringToDouble("1000500100900"))); assertThat(42.0, is(SafeParse.stringToDouble("42"))); assertThat(0.0, is(SafeParse.stringToDouble("12 U/h"))); assertThat(0.0, is(SafeParse.stringToDouble("ala ma kota"))); assertThat(0.0, is(SafeParse.stringToDouble("0.1234.5678"))); assertThat(0.0, is(SafeParse.stringToDouble("0,1234,5678"))); assertThat(0.0, is(SafeParse.stringToDouble(""))); assertThat(0.0, is(SafeParse.stringToDouble(" "))); assertThat(0.0, is(SafeParse.stringToDouble("\n\r"))); }
@Test(expected=NullPointerException.class) public void stringToDoubleNullTest() { SafeParse.stringToDouble(null); }