TypeConverter { public static Double convertToDouble(final Object value) throws TypeConversionException { if (value instanceof Number) { return ((Number) value).doubleValue(); } if (value instanceof String) { if (((String) value).isEmpty()) { return 0.0; } try { return Double.valueOf((String) value); } catch (NumberFormatException ex) { return Double.NaN; } } if (value instanceof Boolean) { return (Boolean) value ? 1.0 : 0.0; } if (value == null) { return Double.NaN; } throw new TypeConversionException("Failed converted to number: unrecognized type of the given value!"); }  static Boolean convertToBool(final Object value); static Double convertToDouble(final Object value);  }
@Test public void should_ConvertValueToDoubleLikeJavaScript() throws TypeConversionException { assertEquals(4.2, convertToDouble(num), 0d); assertEquals(1d, convertToDouble(_true), 0d); assertEquals(0d, convertToDouble(_false), 0d); assertEquals(0d, convertToDouble(zero), 0d); assertEquals(0d, convertToDouble(emptyStr), 0d); assertEquals(Double.NaN, convertToDouble(_null), 0d); assertEquals(Double.NaN, convertToDouble(nan), 0d); assertEquals(Double.NaN, convertToDouble(nonEmptyStr), 0d); }
@Test(expected = TypeConversionException.class) public void should_ThrowException_WithReason_UnrecognizedTypeForConvertToDouble() throws TypeConversionException { convertToDouble(nonNull); }