divide two BigDecimal value - Java java.math

Java examples for java.math:BigDecimal Calculation

Description

divide two BigDecimal value

Demo Code


//package com.java2s;
import java.math.BigDecimal;

public class Main {
    public static void main(String[] argv) throws Exception {
        BigDecimal amount = new BigDecimal("1234");
        BigDecimal divisor = new BigDecimal("1234");
        System.out.println(divide(amount, divisor));
    }/*  ww w  . j  av a2 s. co  m*/

    private static final int LONGSCALE = 6;

    /**
     * ?
     * 
     * @param amount
     * @param divisor
     * @return
     */
    public static BigDecimal divide(BigDecimal amount, BigDecimal divisor) {
        BigDecimal result = null;
        if (divisor == null || divisor.intValue() == 0) {
            return null;
        }
        if (amount != null) {
            result = amount.divide(divisor, LONGSCALE,
                    BigDecimal.ROUND_HALF_UP);
        }
        return result;
    }
}

Related Tutorials