Yakhoob
Yakhoob

Reputation: 569

Check percentage of between two big decimal values using Java

Im having two Big decimal values 1. Salary 2. EMI, I'm trying to calculate the percentage of EMI in the salary. Please find the code .

public static final BigDecimal ONE_HUNDRED = new BigDecimal(100);   

public static void main(String[] args)  {
    BigDecimal salary = new BigDecimal("10000");
    BigDecimal emi = new BigDecimal("7000");
    System.out.println(salary.multiply(emi).divide(ONE_HUNDRED));
}

Output should be 70, because 7000 is 70% of 10000.

Is there any problem in the above?

P.S : Im getting those two values as Bigdecimal only from External web service. Please dont ask to use Float or Integers.

Upvotes: 1

Views: 5033

Answers (1)

Ray Toal
Ray Toal

Reputation: 88428

You are very close!

The last line should be:

System.out.println(emi.multiply(ONE_HUNDRED).divide(salary));

This will give you

7000 * 100 / 10000 = 70

Upvotes: 5

Related Questions