Currently I have a Proportionate Value of 0.588235294 with that I need to multiply it and sum of total value must be 100. With Excel the calculation is working as perfect but in same when i tried with help of BigDecmial api there is a point variations.
The below screenshot is the output from Excel .
In Java
public class CalculateRates {
public static void main(String[] args) {
BigDecimal f1 = new BigDecimal(65);
BigDecimal f2 = new BigDecimal(45);
BigDecimal f3 = new BigDecimal(35);
BigDecimal f4 = new BigDecimal(25);
BigDecimal totalRate = f1.add(f2).add(f3).add(f4);
System.out.println("Total Value : " + totalRate);
BigDecimal proportionatedValue = new BigDecimal(100.00).divide(totalRate, 9, BigDecimal.ROUND_DOWN);
System.out.println("ProportionatedValue : "+proportionatedValue);
BigDecimal f11 = f1.multiply(proportionatedValue).setScale(8);
BigDecimal f12 = f2.multiply(proportionatedValue).setScale(8);
BigDecimal f13 = f3.multiply(proportionatedValue).setScale(8);
BigDecimal f14 = f4.multiply(proportionatedValue).setScale(8);
System.out.println(f11 + ":: " + f12 + ":: " + f13 + ":: " + f14 );
System.out.println("Total : "+f11.add(f12).add(f13).add(f14));
}
}
The output of first and second value is different by one digit at the last
What causing this issue? and How we can approach it for a common solution.
您正在使用BigDecimal.ROUND_DOWN
,如果您确实希望具有与Excel工作表中完全相同的值,则应使用ROUND_UP
。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.