CurrencyBigDecimalConverter extends BigDecimalConverter { @Override public Result<BigDecimal> convertToModel(String value, ValueContext context) { if (value == null) { return Result.ok(null); } value = value.replace(" ", ""); if (!StringUtils.isEmpty(value) && !value.startsWith(currencySymbol)) { value = currencySymbol + value; } return super.convertToModel(value, context); } CurrencyBigDecimalConverter(String message, int precision, boolean useGrouping, String currencySymbol); @Override Result<BigDecimal> convertToModel(String value, ValueContext context); String getCurrencySymbol(); void setCurrencySymbol(String currencySymbol);  }
@Test public void testConvertToModel() { CurrencyBigDecimalConverter cv = new CurrencyBigDecimalConverter("message", 2, true, "€"); assertEquals(123456, cv.convertToModel(cv.getDecimalFormat(new Locale("nl")).getPositivePrefix() + "123456", createContext()) .getOrThrow(r -> new OCSRuntimeException()).doubleValue(), 0.001); assertEquals(123456, cv.convertToModel("123456", createContext()).getOrThrow(r -> new OCSRuntimeException()).doubleValue(), 0.001); assertEquals(123456, cv.convertToModel("€ 123" + symbols.getGroupingSeparator() + "456", createContext()) .getOrThrow(r -> new OCSRuntimeException()).doubleValue(), 0.001); assertEquals(123456, cv.convertToModel("€ 123456", createContext()).getOrThrow(r -> new OCSRuntimeException()).doubleValue(), 0.001); assertEquals(123456.12, cv.convertToModel("€ 123456" + symbols.getDecimalSeparator() + "12", createContext()) .getOrThrow(r -> new OCSRuntimeException()).doubleValue(), 0.001); }
@Test public void testConvertToModelUSA() { DecimalFormatSymbols usa = DecimalFormatSymbols.getInstance(Locale.US); CurrencyBigDecimalConverter cv = new CurrencyBigDecimalConverter("message", 2, true, "$"); assertEquals(123456, cv.convertToModel(cv.getDecimalFormat(Locale.US).getPositivePrefix() + "123456", createUsContext()) .getOrThrow(r -> new OCSRuntimeException()).doubleValue(), 0.001); assertEquals(123456, cv.convertToModel("$123456", createUsContext()).getOrThrow(r -> new OCSRuntimeException()).doubleValue(), 0.001); assertEquals(123456, cv.convertToModel("$123456", createUsContext()).getOrThrow(r -> new OCSRuntimeException()).doubleValue(), 0.001); assertEquals(123456.12, cv.convertToModel("$123" + usa.getGroupingSeparator() + "456" + usa.getDecimalSeparator() + "12", createUsContext()) .getOrThrow(r -> new OCSRuntimeException()).doubleValue(), 0.001); }