subtract one BigDecimal from another BigDecimal - Java java.math

Java examples for java.math:BigDecimal Calculation

Description

subtract one BigDecimal from another BigDecimal

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 subtrahend = new BigDecimal("1234");
        System.out.println(subtract(amount, subtrahend));
    }//from www . ja  va  2  s. co m

    public static BigDecimal subtract(BigDecimal amount,
            BigDecimal subtrahend) {
        BigDecimal result = new BigDecimal("0.00");
        if (amount != null) {
            result = result.add(amount);
        }
        if (subtrahend != null) {
            result = result.subtract(subtrahend);
        }
        return result;
    }

    /**
     * ?
     * 
     * @param amount1
     * @param amount2
     * @return
     */
    public static BigDecimal add(BigDecimal amount1, BigDecimal amount2) {
        BigDecimal result = new BigDecimal("0.00");
        if (amount1 != null) {
            result = result.add(amount1);
        }
        if (amount2 != null) {
            result = result.add(amount2);
        }
        return result;
    }
}

Related Tutorials