@Test public void testEmtpy() { String numString = ""; BigDecimal parsed = BigDecimalUtil.parseFromNumberString(numString); assertNull("The parsed value is not null.", parsed); }
public static BigDecimal parseFromNumberString(@Nullable String numberString) { BigDecimal result = null; if (numberString != null) { String nonSpacedString = numberString.replaceAll("[ \\t\\n\\x0B\\f\\r%]", ""); if (!Strings.isNullOrEmpty(nonSpacedString)){ int indexOfComma = nonSpacedString.indexOf(','); int indexOfDot = nonSpacedString.indexOf('.'); DecimalFormat decimalFormat = (DecimalFormat) NumberFormat.getInstance(); DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(); if (indexOfComma < indexOfDot) { otherSymbols.setDecimalSeparator('.'); otherSymbols.setGroupingSeparator(','); } else { otherSymbols.setDecimalSeparator(','); otherSymbols.setGroupingSeparator('.'); } decimalFormat.setParseBigDecimal(true); decimalFormat.setDecimalFormatSymbols(otherSymbols); try { result = (BigDecimal) decimalFormat.parse(nonSpacedString); } catch (ParseException e) { throw new RuntimeException("Error parsing '" + nonSpacedString + "' as BigDecimal", e); } } } return result; }
BigDecimalUtil { public static BigDecimal parseFromNumberString(@Nullable String numberString) { BigDecimal result = null; if (numberString != null) { String nonSpacedString = numberString.replaceAll("[ \\t\\n\\x0B\\f\\r%]", ""); if (!Strings.isNullOrEmpty(nonSpacedString)){ int indexOfComma = nonSpacedString.indexOf(','); int indexOfDot = nonSpacedString.indexOf('.'); DecimalFormat decimalFormat = (DecimalFormat) NumberFormat.getInstance(); DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(); if (indexOfComma < indexOfDot) { otherSymbols.setDecimalSeparator('.'); otherSymbols.setGroupingSeparator(','); } else { otherSymbols.setDecimalSeparator(','); otherSymbols.setGroupingSeparator('.'); } decimalFormat.setParseBigDecimal(true); decimalFormat.setDecimalFormatSymbols(otherSymbols); try { result = (BigDecimal) decimalFormat.parse(nonSpacedString); } catch (ParseException e) { throw new RuntimeException("Error parsing '" + nonSpacedString + "' as BigDecimal", e); } } } return result; } }
BigDecimalUtil { public static BigDecimal parseFromNumberString(@Nullable String numberString) { BigDecimal result = null; if (numberString != null) { String nonSpacedString = numberString.replaceAll("[ \\t\\n\\x0B\\f\\r%]", ""); if (!Strings.isNullOrEmpty(nonSpacedString)){ int indexOfComma = nonSpacedString.indexOf(','); int indexOfDot = nonSpacedString.indexOf('.'); DecimalFormat decimalFormat = (DecimalFormat) NumberFormat.getInstance(); DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(); if (indexOfComma < indexOfDot) { otherSymbols.setDecimalSeparator('.'); otherSymbols.setGroupingSeparator(','); } else { otherSymbols.setDecimalSeparator(','); otherSymbols.setGroupingSeparator('.'); } decimalFormat.setParseBigDecimal(true); decimalFormat.setDecimalFormatSymbols(otherSymbols); try { result = (BigDecimal) decimalFormat.parse(nonSpacedString); } catch (ParseException e) { throw new RuntimeException("Error parsing '" + nonSpacedString + "' as BigDecimal", e); } } } return result; } private  BigDecimalUtil(); }
BigDecimalUtil { public static BigDecimal parseFromNumberString(@Nullable String numberString) { BigDecimal result = null; if (numberString != null) { String nonSpacedString = numberString.replaceAll("[ \\t\\n\\x0B\\f\\r%]", ""); if (!Strings.isNullOrEmpty(nonSpacedString)){ int indexOfComma = nonSpacedString.indexOf(','); int indexOfDot = nonSpacedString.indexOf('.'); DecimalFormat decimalFormat = (DecimalFormat) NumberFormat.getInstance(); DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(); if (indexOfComma < indexOfDot) { otherSymbols.setDecimalSeparator('.'); otherSymbols.setGroupingSeparator(','); } else { otherSymbols.setDecimalSeparator(','); otherSymbols.setGroupingSeparator('.'); } decimalFormat.setParseBigDecimal(true); decimalFormat.setDecimalFormatSymbols(otherSymbols); try { result = (BigDecimal) decimalFormat.parse(nonSpacedString); } catch (ParseException e) { throw new RuntimeException("Error parsing '" + nonSpacedString + "' as BigDecimal", e); } } } return result; } private  BigDecimalUtil(); static BigDecimal parseFromNumberString(@Nullable String numberString); static BigDecimal parseFromNumberStringPercentage(@Nullable String numberString); static String formatAsReadable(@Nullable BigDecimal number); static BigDecimal parseFromMoneyString(@Nullable String numberString); }
BigDecimalUtil { public static BigDecimal parseFromNumberString(@Nullable String numberString) { BigDecimal result = null; if (numberString != null) { String nonSpacedString = numberString.replaceAll("[ \\t\\n\\x0B\\f\\r%]", ""); if (!Strings.isNullOrEmpty(nonSpacedString)){ int indexOfComma = nonSpacedString.indexOf(','); int indexOfDot = nonSpacedString.indexOf('.'); DecimalFormat decimalFormat = (DecimalFormat) NumberFormat.getInstance(); DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(); if (indexOfComma < indexOfDot) { otherSymbols.setDecimalSeparator('.'); otherSymbols.setGroupingSeparator(','); } else { otherSymbols.setDecimalSeparator(','); otherSymbols.setGroupingSeparator('.'); } decimalFormat.setParseBigDecimal(true); decimalFormat.setDecimalFormatSymbols(otherSymbols); try { result = (BigDecimal) decimalFormat.parse(nonSpacedString); } catch (ParseException e) { throw new RuntimeException("Error parsing '" + nonSpacedString + "' as BigDecimal", e); } } } return result; } private  BigDecimalUtil(); static BigDecimal parseFromNumberString(@Nullable String numberString); static BigDecimal parseFromNumberStringPercentage(@Nullable String numberString); static String formatAsReadable(@Nullable BigDecimal number); static BigDecimal parseFromMoneyString(@Nullable String numberString);  }