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);  }
@Test public void testBrazilianNumberFormat() { String numString = "1 200 300,10"; BigDecimal parsed = BigDecimalUtil.parseFromNumberString(numString); assertEquals( "The parsed value is not the expected.", 1200300.10, parsed.doubleValue(), 1e-6); numString = "1.200.300,10"; parsed = BigDecimalUtil.parseFromNumberString(numString); assertEquals( "The parsed value is not the expected.", 1200300.10, parsed.doubleValue(), 1e-6); }
@Test public void testRoundingComma() { String numString = "0,39"; BigDecimal parsed = BigDecimalUtil.parseFromNumberString(numString); assertEquals("The parsed value is not the expected.", 0.39, parsed.doubleValue(), 0.0); numString = "33,33"; parsed = BigDecimalUtil.parseFromNumberString(numString); assertEquals("The parsed value is not the expected.", 33.33, parsed.doubleValue(), 0.0); }
@Test public void testRoundingDot() { String numString = "0.39"; BigDecimal parsed = BigDecimalUtil.parseFromNumberString(numString); assertEquals("The parsed value is not the expected.", 0.39, parsed.doubleValue(), 0.0); numString = "33.33"; parsed = BigDecimalUtil.parseFromNumberString(numString); assertEquals("The parsed value is not the expected.", 33.33, parsed.doubleValue(), 0.0); }
@Test public void testPercent() { String numString = "0,39%"; BigDecimal parsed = BigDecimalUtil.parseFromNumberString(numString); assertEquals("The parsed value is not the expected.", 0.39, parsed.doubleValue(), 0.0); numString = "33,33%"; parsed = BigDecimalUtil.parseFromNumberString(numString); assertEquals("The parsed value is not the expected.", 33.33, parsed.doubleValue(), 0.0); }
@Test public void testEmtpy() { String numString = ""; BigDecimal parsed = BigDecimalUtil.parseFromNumberString(numString); assertNull("The parsed value is not null.", parsed); }
@Test public void testNull() { String numString = null; BigDecimal parsed = BigDecimalUtil.parseFromNumberString(numString); assertNull("The parsed value is not null.", parsed); }
@Test(expected = RuntimeException.class) public void testNotANumber() { String numString = "abcdef"; BigDecimalUtil.parseFromNumberString(numString); }
@Test public void testAmericanNumberFormat() { String numString = "1 200 300.10"; BigDecimal parsed = BigDecimalUtil.parseFromNumberString(numString); assertEquals( "The parsed value is not the expected.", 1200300.10, parsed.doubleValue(), 1e-6); numString = "1,200,300.10"; parsed = BigDecimalUtil.parseFromNumberString(numString); assertEquals( "The parsed value is not the expected.", 1200300.10, parsed.doubleValue(), 1e-6); }