0

I have this result of division:

4893.305785123966785163027491292661448576748

I want to round to 4893.30, but it always returns 4893.31. but when the second decimal will be 6 or more i want to round up.

new BigDecimal("4893.305785123966785163027491292661448576748").setScale(2,BigDecimal.ROUND_HALF_DOWN)); to 4893.31 //BAD

new BigDecimal("4893.305").setScale(2,BigDecimal.ROUND_HALF_DOWN)); to 4893.30 //OK

new BigDecimal("4893.3055").setScale(2,BigDecimal.ROUND_HALF_DOWN)); to 4893.31 //BAD

How does the round work? I want to take in consideration only the value of the second decimal. Any ideas?

Ignacio
  • 121
  • 3
  • 10
  • 1
    The result you want is a truncation, not a rounding. – user207421 Jan 16 '15 at 21:16
  • 1
    ROUND_HALF_DOWN means exactly that: if the part being "removed" represents 1/2 or less, truncate; otherwise, round up. In the case you think there is a problem, the part being "removed" represents more than 1/2. – Scott Hunter Jan 16 '15 at 21:18
  • When the second decimal digit will be 6 or more i want to round up that why im using rounding. – Ignacio Jan 16 '15 at 21:24
  • 3
    What you're looking for is not a standard kind of rounding. "Rounding" usually either always rounds up or down, or it rounds to the *nearest* desired value (with different options for how values that are **exactly halfway** between two desired values). 4893.30578 is closer to 4893.31 than 4893.30, that's why it rounds to 4893.31. If you think you want 4893.30578 to round to 4893.30, but 4893.3061 to round to 4893.31, either you have misunderstood what rounding is, or you have some very unusual requirements. – ajb Jan 16 '15 at 21:33
  • Read doc http://docs.oracle.com/javase/1.5.0/docs/api/java/math/BigDecimal.html to know how exactly it works.... and rounding modes http://docs.oracle.com/javase/1.5.0/docs/api/java/math/BigDecimal.html#ROUND_DOWN – Naruto Dec 04 '15 at 16:42

2 Answers2

1

Perhaps you're looking for a truncate function at the given scale? The BigDecimal rounding you show is working exactly as designed.

Brian Knoblauch
  • 20,639
  • 15
  • 57
  • 92
1

"How does the round work? I want to take in consideration only the value of the second decimal."

It sounds like you want to truncate instead of round.

Instead of using ROUND_HALF_DOWN:

Rounding mode to round towards "nearest neighbor"...

...instead, try using ROUND_DOWN:

Rounding mode to round towards zero. Never increments the digit prior to a discarded fraction (i.e., truncates)...

e.g.

    BigDecimal bd = new BigDecimal("4893.305785123966785163027491292661448576748");
    bd = bd.setScale(2, BigDecimal.ROUND_DOWN);
    System.out.println(bd); // 4893.30

Update to answer question edit

If you want to only take certain decimal places into consideration, that's a truncate followed by a rounding:

    BigDecimal bd = new BigDecimal("4893.305785123966785163027491292661448576748");
    bd = bd.setScale(3, BigDecimal.ROUND_DOWN);       // truncate to 4893.305
    bd = bd.setScale(2, BigDecimal.ROUND_HALF_DOWN);  // round to 2 decimal places
    System.out.println(bd);                           // 4893.30

    bd = new BigDecimal("4893.306785123966785163027491292661448576748");
    bd = bd.setScale(3, BigDecimal.ROUND_DOWN);       // truncate to 4893.306
    bd = bd.setScale(2, BigDecimal.ROUND_HALF_DOWN);  // round to 2 decimal places
    System.out.println(bd);                           // 4893.31
azurefrog
  • 10,785
  • 7
  • 42
  • 56