Mauro M
Mauro M

Reputation: 669

Java BigDecimal Rounding

I am learning BigDecimal and i want it to retrieve the exact number i entered, the following code is rouding the number and i dont know why

public static BigDecimal parseFromNumberString(String numberString) {

    if (numberString != null) {

        String nonSpacedString =
            numberString.replaceAll("[ \\t\\n\\x0B\\f\\r]", "").replaceAll("%", "");

        int indexOfComma = nonSpacedString.indexOf(',');
        int indexOfDot = nonSpacedString.indexOf('.');
        NumberFormat format = null;

        if (indexOfComma < indexOfDot) {
            nonSpacedString = nonSpacedString.replaceAll("[,]", "");
            format = new DecimalFormat("##.#");
        } else if (indexOfComma > indexOfDot) {
            nonSpacedString = nonSpacedString.replaceAll("[.]", "");    
            DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols();
            otherSymbols.setDecimalSeparator(',');
            format = new DecimalFormat("##,#", otherSymbols);
        } else {
            format = new DecimalFormat();
        }
        try {
            return new BigDecimal(format.parse(nonSpacedString).doubleValue(), new MathContext(12));
        } catch (ParseException e) {
            // unrecognized number format
            return null;
        }
    }
    return null;
}

If i do something like

public static void main(String[] args){
    BigDecimal d = Test.parseFromNumberString("0.39");
    System.out.println(d);  
}

The value printed is 0,00 and not 0.39

Upvotes: 0

Views: 180

Answers (2)

Light1988
Light1988

Reputation: 77

Try this code:

public static BigDecimal parseFromNumberString(String numberString) {

    if (numberString != null) {

        String nonSpacedString =
            numberString.replaceAll("[ \\t\\n\\x0B\\f\\r]", "").replaceAll("%", "");

        int indexOfComma = nonSpacedString.indexOf(',');
        int indexOfDot = nonSpacedString.indexOf('.');
        DecimalFormat decimalFormat = new DecimalFormat();
        DecimalFormatSymbols symbols = new DecimalFormatSymbols();
        String pattern = "#0.0#";           

        if (indexOfComma < indexOfDot) {
            symbols.setDecimalSeparator('.');
        } else if (indexOfComma > indexOfDot) {
            symbols.setDecimalSeparator(',');
        } 

        try {
            decimalFormat = new DecimalFormat(pattern, symbols);
            decimalFormat.setParseBigDecimal(true);
            BigDecimal toRet = (BigDecimal) decimalFormat.parse(nonSpacedString);
            return toRet.setScale(12);
        } catch (ParseException e) {
            return null;
        }
    }
    return null;
}

public static void main(String... args) {
    BigDecimal d = Test.parseFromNumberString("0,39");
    System.out.println(d);  
}

Is that what you want??

Upvotes: 1

Dima
Dima

Reputation: 8662

i just ran your code and i get. 0.390000000000 maybe you forgot to save?

try cleanning your project, restart your ide and recompile. the code should work fine

Upvotes: 0

Related Questions