PercentageBigDecimalConverter extends BigDecimalConverter { @Override public Result<BigDecimal> convertToModel(String value, ValueContext context) { value = value == null ? null : value.replace("%", ""); return super.convertToModel(value, context); } PercentageBigDecimalConverter(String message, int precision, boolean useGrouping); @Override String convertToPresentation(BigDecimal value, ValueContext context); @Override Result<BigDecimal> convertToModel(String value, ValueContext context);  }
@Test public void testConvertToModel() { BigDecimalConverter converter = new PercentageBigDecimalConverter("message", 2, false); Result<BigDecimal> result = converter.convertToModel("3,14%", new ValueContext(new Locale("nl"))); assertEquals(BigDecimal.valueOf(3.14).setScale(2, RoundingMode.HALF_EVEN), result.getOrThrow(r -> new OCSRuntimeException())); result = converter.convertToModel("3,14", new ValueContext(new Locale("nl"))); assertEquals(BigDecimal.valueOf(3.14).setScale(2, RoundingMode.HALF_EVEN), result.getOrThrow(r -> new OCSRuntimeException())); converter = new PercentageBigDecimalConverter("message", 2, false); result = converter.convertToModel("3.14%", new ValueContext(Locale.US)); assertEquals(BigDecimal.valueOf(3.14).setScale(2, RoundingMode.HALF_EVEN), result.getOrThrow(r -> new OCSRuntimeException())); assertNull(converter.convertToModel(null, new ValueContext(new Locale("nl"))).getOrThrow(r -> new OCSRuntimeException())); }