2

I am using java big decimal as follwing:

class test bigDecimal{

private BigDecimal decimalResult;


    public boolean iterate(String value) {
        if (value == null) {
            return true;
        }

        System.out.println("value is: " + value);

        BigDecimal temp = new BigDecimal(value);

        System.out.println("temp val is: " + temp);
        if (decimalResult == null) {
            decimalResult = temp;
        } else {
            decimalResult = decimalResult.add(temp);
        }

        return true;
    }

}

all the strings that I am using to create big Decimal have scale of 6. For eg: 123456.678000, 456789.567890

But If I give a big list of strings as input and check the sum, I get output with 8 digits after decimal point. for eg. something like: 2939166.38847228.

I wonder why does BigDecimal change this scale ? all my input has scale of 6.

Any inputs are appreciated. -thanks

3
  • 1
    Can you post an SSCCE that shows this? Commented Jul 12, 2011 at 17:02
  • lol @ Any inputs are appreciated :) Commented Jul 12, 2011 at 17:03
  • Inputs are about 1 million such stribg values that are getting summed up as big decimal and getting printed as strings. Commented Jul 12, 2011 at 17:54

3 Answers 3

6

Please read the javadoc for BigDecimal: http://download.oracle.com/javase/6/docs/api/java/math/BigDecimal.html Internally all BigDecimals get converted to a some internal format.

If you want to get a specific format you have to call it appropriate. You could use scaleByPowerOfTen(int n) with n=6 for example

Oh, and by the way never use new BigDecimal, use BigDecimal.valueOf instead.

EDIT:

NumberFormat numberFormat = NumberFormat.getInstance(); // use a locale here String formated = df.format(myUnformatedBigDecimal);

Sign up to request clarification or add additional context in comments.

1 Comment

Regarding not using new, I have a string that I am using to create big decimal from string. valueOf only accepts long/double as input. In my case, input can be 21 characters that is more than what a long/double will hold.
0

It is not a problem of internal representation. This way the BigDecimal.toString() works. It calls layoutChars(true) - the private method that formats number. It has hard coded scale of 6. But I think it does not matter. You do not really have to pint all digits. BigDecimal provides ability to calculate high precision numbers. That's what it does.

1 Comment

Sorry, you're absolutly right. I thought of the internal representation of BigInteger. You should use something like this: NumberFormat numberFormat = NumberFormat.getInstance(); // use a locale here String formated = df.format(myUnformatedBigDecimal);
0

Big decimal is giving an exact output, but that output is more accurate than 6 digits, so the best way to get 6 digit output is removing the last 2 decimals by converting the BigDecimal into string and doing something like that:

    String myOutput = new 
    BigDecimal("1000.12345678").toString();
    myOutput = myOutput.substring(myOutput.length-2, 
     myOutput.length);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.