Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

199
Views
BigDecimal divide with scale

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?

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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();
}
about 4 years ago · Santiago Trujillo Report

0

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.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!