13

I have

private static final BigDecimal ONE_HUNDRED = new BigDecimal(100);
    private static final BigDecimal TEN = new BigDecimal(10);

BigDecimal decimal = new BigDecimal(1050); I need to get 10% I write BigDecimal decimalResult = decimal.divide(ONE_HUNDRED).multiply(TEN)//100, 10

But Intellij IDE says:

'BigDecimal.divide()' called without a rounding mode argument more...

I added BigDecimal.ROUND_HALF_UP and all others but I get wrong result. I need 1050/100 = 10.5 but if I add BigDecimal.ROUND_HALF_UP result = 11.

How can I correctly divide with scale parameters?

user207421
  • 305,947
  • 44
  • 307
  • 483
user5620472
  • 2,722
  • 8
  • 44
  • 97
  • 1
    maybe you should read: http://stackoverflow.com/questions/35435691/bigdecimal-precision-and-scale –  Apr 03 '17 at 11:19
  • What are `ONE_HUNDRED` and `TEN`? – RealSkeptic Apr 03 '17 at 11:25
  • `TEN` and `ONE_HUNDRED` does not exists as constants in `BigDecimal` – freedev Apr 03 '17 at 11:31
  • Define 'more ...'. – user207421 Apr 03 '17 at 11:31
  • As in my answer, you could specify the scale during creation of `BigDecimal` – freedev Apr 03 '17 at 11:43
  • 1
    I faund solution. BigDecimal recovery = amount.divide(ONE_HUNDRED, 2, BigDecimal.ROUND_HALF_UP) But @ freedev - Why TEN and ONE_HUNDRED does not exists as constants in BigDecimal? I created constans myself – user5620472 Apr 03 '17 at 11:46
  • @user5620472 Good solution. Note that freedev commented prior to your edit: in the previous version you didn't show what TEN and ONE_HUNDRED were. By the way, TEN _is_ defined in BigDecimal and therefore you don't need to define it yourself. Also, it's better to write `BigDecimal.valueOf(x)` than `new BigDecimal(x)`. – Klitos Kyriacou Apr 03 '17 at 12:06
  • Please, note that for a good result, you should always multiply before you divide, even with BigDecimals. But IntelliJ is simply wrong. You *can* use `divide()` without a rounding mode. You just risk an error if the result would have to create a non-terminating repetitive fraction, but that is rather unlikely if you divide by 100. Dividing by 3 would give you an error, if you don't give a rounding mode. – Rudy Velthuis Apr 03 '17 at 22:48
  • @RudyVelthuis IntelliJ is not wrong. It doesn't give you an error; it's just a friendly warning because calling `divide()` without supplying a rounding mode is a common cause of logic error, usually due to failure to think properly about rounding. If you know what you are doing and are deliberately calling `divide()` without a rounding mode, you can then safely ignore the warning or turn it off. – Klitos Kyriacou Apr 04 '17 at 12:28

2 Answers2

15
public static double divide(double d1, double d2) {
    BigDecimal b1 = new BigDecimal(d1);
    BigDecimal b2 = new BigDecimal(d2);
    return b1.divide(b2, 2, RoundingMode.DOWN).doubleValue();
}
panqingcui
  • 159
  • 1
  • 6
2

This example returns 105.0000

public class TestBigDecimal {
  static BigDecimal decimal = new BigDecimal(1050).setScale(4, BigDecimal.ROUND_HALF_UP);
  static BigDecimal ONE_HUNDRED = new BigDecimal(100);
  static BigDecimal TEN = new BigDecimal(10);

  public static void main(String[] args)
  {
    BigDecimal decimalResult = decimal.divide(ONE_HUNDRED).multiply(TEN) ;
    System.out.println(decimalResult);
  }
}

You should adjust the scale during the creation of decimal variable.

freedev
  • 25,946
  • 8
  • 108
  • 125
  • Intellij IDE is still saying: "'BigDecimal.divide()' called without a rounding mode argument more..." I don't think your answer answers the question –  Apr 03 '17 at 11:23
  • 3
    You know, TEN already exists as a constant in the `BigDecimal` class. – RealSkeptic Apr 03 '17 at 11:24
  • @RC right, I think this answer should fit the problem. – freedev Apr 03 '17 at 11:42
  • What exactly does IntelliJ say? what is the text *after* the `more...`part? Because you *can* use `divide()` without a rounding mode, and it is entirely safe if you use ONE_HUNDRED as argument. – Rudy Velthuis Apr 03 '17 at 22:53