BigDecimalConverter extends StringToBigDecimalConverter { @Override public Result<BigDecimal> convertToModel(String value, ValueContext context) { if (value == null) { return Result.ok(null); } Result<Number> number = convertToNumber(value, context); return number.flatMap(r -> { BigDecimal bd = r == null ? null : BigDecimal.valueOf(r.doubleValue()).setScale(precision, RoundingMode.HALF_UP); return Result.ok(bd); }); } BigDecimalConverter(String message, int precision, boolean useGrouping); @Override Result<BigDecimal> convertToModel(String value, ValueContext context); DecimalFormat getDecimalFormat(Locale locale);  }
@Test public void testConvertToModel() { BigDecimalConverter converter = new BigDecimalConverter("message", 2, false); Result<BigDecimal> result = converter.convertToModel("3,14", createContext()); assertEquals(BigDecimal.valueOf(3.14).setScale(2, RoundingMode.HALF_EVEN), result.getOrThrow(x -> new OCSRuntimeException())); converter = new BigDecimalConverter("message", 2, false); result = converter.convertToModel("3.142", createUsContext()); assertEquals(BigDecimal.valueOf(3.14).setScale(2, RoundingMode.HALF_EVEN), result.getOrThrow(x -> new OCSRuntimeException())); converter = new BigDecimalConverter("message", 3, false); result = converter.convertToModel("3.142", createUsContext()); assertEquals(3.142, result.getOrThrow(x -> new OCSRuntimeException()).doubleValue(), 0.0001); converter = new BigDecimalConverter("message", 0, false); result = converter.convertToModel("3.142", createUsContext()); assertEquals(3, result.getOrThrow(x -> new OCSRuntimeException()).doubleValue(), 0.0001); }