@Test public final void shouldParseNotEmptyPlDecimalString() { String stringValue = "1,2"; Either<Exception, Optional<BigDecimal>> res = BigDecimalUtils.tryParse(stringValue, new Locale("pl")); Assert.assertEquals(Either.right(Optional.of(new BigDecimal("1.2"))), res); }
public static Either<Exception, Optional<BigDecimal>> tryParse(final String maybeStringWithDecimal, final Locale locale) { if (StringUtils.isBlank(maybeStringWithDecimal)) { return Either.right(Optional.<BigDecimal>absent()); } try { DecimalFormat format = (DecimalFormat) DecimalFormat.getInstance(locale); format.setParseBigDecimal(true); BigDecimal parsedDecimal = new BigDecimal(format.parse(maybeStringWithDecimal).toString()); return Either.right(Optional.fromNullable(parsedDecimal)); } catch (Exception e) { return Either.left(e); } }
BigDecimalUtils { public static Either<Exception, Optional<BigDecimal>> tryParse(final String maybeStringWithDecimal, final Locale locale) { if (StringUtils.isBlank(maybeStringWithDecimal)) { return Either.right(Optional.<BigDecimal>absent()); } try { DecimalFormat format = (DecimalFormat) DecimalFormat.getInstance(locale); format.setParseBigDecimal(true); BigDecimal parsedDecimal = new BigDecimal(format.parse(maybeStringWithDecimal).toString()); return Either.right(Optional.fromNullable(parsedDecimal)); } catch (Exception e) { return Either.left(e); } } }
BigDecimalUtils { public static Either<Exception, Optional<BigDecimal>> tryParse(final String maybeStringWithDecimal, final Locale locale) { if (StringUtils.isBlank(maybeStringWithDecimal)) { return Either.right(Optional.<BigDecimal>absent()); } try { DecimalFormat format = (DecimalFormat) DecimalFormat.getInstance(locale); format.setParseBigDecimal(true); BigDecimal parsedDecimal = new BigDecimal(format.parse(maybeStringWithDecimal).toString()); return Either.right(Optional.fromNullable(parsedDecimal)); } catch (Exception e) { return Either.left(e); } } private  BigDecimalUtils(); }
BigDecimalUtils { public static Either<Exception, Optional<BigDecimal>> tryParse(final String maybeStringWithDecimal, final Locale locale) { if (StringUtils.isBlank(maybeStringWithDecimal)) { return Either.right(Optional.<BigDecimal>absent()); } try { DecimalFormat format = (DecimalFormat) DecimalFormat.getInstance(locale); format.setParseBigDecimal(true); BigDecimal parsedDecimal = new BigDecimal(format.parse(maybeStringWithDecimal).toString()); return Either.right(Optional.fromNullable(parsedDecimal)); } catch (Exception e) { return Either.left(e); } } private  BigDecimalUtils(); static BigDecimal convertNullToZero(final Object value); static BigDecimal convertNullToOne(final Object value); static BigDecimal toPercent(final BigDecimal decimalValue, final MathContext mathContext); static boolean valueEquals(final BigDecimal d1, final BigDecimal d2); static Either<Exception, Optional<BigDecimal>> tryParse(final String maybeStringWithDecimal, final Locale locale); static Either<Exception, Optional<BigDecimal>> tryParseAndIgnoreSeparator(final String maybeStringWithDecimal, final Locale locale); static boolean checkIfCorrectDecimalValue(final Entity entity, final String decimalFieldName); static String toString(BigDecimal decimal,int maximumFractionDigits); }
BigDecimalUtils { public static Either<Exception, Optional<BigDecimal>> tryParse(final String maybeStringWithDecimal, final Locale locale) { if (StringUtils.isBlank(maybeStringWithDecimal)) { return Either.right(Optional.<BigDecimal>absent()); } try { DecimalFormat format = (DecimalFormat) DecimalFormat.getInstance(locale); format.setParseBigDecimal(true); BigDecimal parsedDecimal = new BigDecimal(format.parse(maybeStringWithDecimal).toString()); return Either.right(Optional.fromNullable(parsedDecimal)); } catch (Exception e) { return Either.left(e); } } private  BigDecimalUtils(); static BigDecimal convertNullToZero(final Object value); static BigDecimal convertNullToOne(final Object value); static BigDecimal toPercent(final BigDecimal decimalValue, final MathContext mathContext); static boolean valueEquals(final BigDecimal d1, final BigDecimal d2); static Either<Exception, Optional<BigDecimal>> tryParse(final String maybeStringWithDecimal, final Locale locale); static Either<Exception, Optional<BigDecimal>> tryParseAndIgnoreSeparator(final String maybeStringWithDecimal, final Locale locale); static boolean checkIfCorrectDecimalValue(final Entity entity, final String decimalFieldName); static String toString(BigDecimal decimal,int maximumFractionDigits);  }