FloatConverter extends AbstractDecimalConverter<Float> { @Override public Float convertToObject(final String value, final Locale locale) throws ConversionException { String nonNumericsToPoints = value.replaceAll("['| ]", "").replaceAll(",", ".").replaceAll("[^0-9|^\\.]", ""); int lastDotAt = nonNumericsToPoints.lastIndexOf("."); if (lastDotAt > -1) { int decimalsBehindPoint = nonNumericsToPoints.length() - nonNumericsToPoints.lastIndexOf("."); String withoutDots = value.replaceAll("[^0-9*]", ""); int insertAt = (withoutDots.length() - decimalsBehindPoint) + 1; String toParse = new StringBuilder(withoutDots).insert(insertAt, '.').toString(); try { return Float.parseFloat(toParse); } catch (NumberFormatException nfe) { throw new ConversionException(nfe); } } else { return parse (value); } }  @Override Float convertToObject(final String value, final Locale locale);  }
@Test public void should_convert_using_point_as_decimal_separator() { assertEquals(12f, converter.convertToObject("12.0", new Locale("nl-NL")), 0.01f); }
@Test public void should_convert_using_point_as_decimal_separator_with_decimal() { assertEquals(12.1f, converter.convertToObject("12.1", new Locale("nl-NL")), 0.01f); }
@Test public void should_convert_using_comma_as_decimal_separator() { assertEquals(12f, converter.convertToObject("12,0", new Locale("nl-NL")), 0.01f); }
@Test public void should_convert_using_comma_as_decimal_separator_with_decimal() { assertEquals(12.1f, converter.convertToObject("12,1", new Locale("nl-NL")), 0.01f); }
@Test public void should_convert_with_thousand_point_separator() { assertEquals(12000.3f, converter.convertToObject("12.000,30", new Locale("nl-NL")), 0.01f); }
@Test public void should_convert_with_thousand_point_separator_french_style() { assertEquals(12000f, converter.convertToObject("12'000,0", new Locale("nl-NL")), 0.01f); }
@Test public void should_convert_with_thousand_point_separator_french_style_and_point_decimal() { assertEquals(12000.5f, converter.convertToObject("12'000.50", new Locale("nl-NL")), 0.01f); }
@Test public void should_convert_without_any_commas() { assertEquals(12f, converter.convertToObject("12", new Locale("nl-NL")), 0.01f); }
@Test public void should_convert_with_space_separator() { assertEquals(12000.1f, converter.convertToObject("12 000,1", new Locale("nl-NL")), 0.01f); }
@Test public void should_ignore_non_numeric_chars() { assertEquals(12000.1f, converter.convertToObject("12 000,1x0", new Locale("nl-NL")), 0.01f); }
@Test public void should_ignore_double_non_numeric_chars() { assertEquals(12000.1f, converter.convertToObject("12 0xyz00,1<x0", new Locale("nl-NL")), 0.01f); }