Java examples for java.math:BigDecimal Calculation
Calculates BigDecimal divided by total
//package com.java2s; import java.math.BigDecimal; public class Main { public static void main(String[] argv) throws Exception { BigDecimal num = new BigDecimal("1234"); int total = 2; System.out.println(divide(num, total)); }/*from w ww . j a v a 2 s .c om*/ /** * Calculates num divided by total3 * e.g. divide(5, 100)= 0.05 * @param num the number to divide * @param total the number to be divided * @return num divided by total */ public static BigDecimal divide(BigDecimal num, int total) { if (total == 0) { throw new IllegalArgumentException( "Total cannot be ZERO when dividing"); } BigDecimal bTotal = new BigDecimal(total); return num.divide(bTotal, 2, BigDecimal.ROUND_HALF_UP); } }