Decimal point rounding

I had the following method for some currency conversion, the program should calculate 50000.0/8185= 6.10873549175321 and round it as 6.11, however i am getting 6.10 no matter what BigDecimal rounding method used.


public String conv2NDCPrtData(){
    BigDecimal amtReqBd = new BigDecimal("50000.0");
    System.out.println("amtReqBd= "+ amtReqBd);
    
    BigDecimal rate1 = new BigDecimal("8185").multiply(new
                   BigDecimal(10).pow(4)) .setScale(0, 
                   BigDecimal.ROUND_HALF_EVEN);
    String dConvRate = rate1.toString();
    BigDecimal convRate = new BigDecimal(dConvRate).divide(new 
                  BigDecimal(10).pow(4));
    System.out.println("convRate= "+ convRate);
             
    BigDecimal convAmt = amtReqBd.divide(convRate,      
                   convRate.precision()).setScale(2, 
                   BigDecimal.ROUND_HALF_EVEN);
    System.out.println("convAmt= "+ convAmt);

    return convAmt;
}

Thanks for helping!

money type is only available at jdk 7 right? We are still using 1.5 for production, does BigDecimal still doe not precise enough in this case?

So after doing some searching on this site, the issues surrounding money are starting to come back… Slowly, really slowly…

Money is strange, since dollars are really just a classification of a grouped amount much like hundreds, thousands, and millions are in normal counting. The deal is that money is really cents. Not dollars. A dollar really = 100 cents. When dealing with a 50 cents you don’t have 1/2 a dollar you have 50 cents. When you have a dollar, you really have 100 cents. When you have a million dollars you really have 100,000,000 (one hundred million) cents.

So the ‘proper’ way to store money is to store how many cents of something you have. Then using formatting you determine how many dollars you have with x amount of cents. So consider storing your money as an Integer instead of a Double or Float which allows rounding. Have you have seen 1/2 a cent? Neither have I because a cent is always a whole number. Storing money as an Integer will also help with calculating exchanges between currencies.

There was a great article or thread on this site talking about money, but I can’t find it. I did find the following threads though:

The first thread has a link to a in-depth discussion about rounding, which might be beneficial to you.

BUT, if you have to store money as a decimal (despite the warning above), I’ll take a look at what is happening in your code a little later tonight or this weekend sometime.

I would have to look closer to see what is going wrong, but if you’re using the above code in a production system you might consider using a money type instead of an integer for keeping track of money.

There are some great articles on this site that explain the use of money types vs integers or even big decimal.

I will have to take time later though to see what the issue is with the rounding.

It’s working now…The rounding value is correct…Thank jurn:)

when you divide with only 2 arguments, the 2nd argument is meant to be the RoundingMode… I think it is unrelated to ‘precision’.
You probably meant

        BigDecimal convAmt = amtReqBd.divide(convRate, 2, RoundingMode.HALF_EVEN);

?