Multiplies first BigDecimal by second BigDecimal. - Java java.math

Java examples for java.math:BigDecimal

Description

Multiplies first BigDecimal by second BigDecimal.

Demo Code


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

public class Main {
    public static void main(String[] argv) throws Exception {
        BigDecimal first = new BigDecimal("1234");
        BigDecimal second = new BigDecimal("1234");
        int scale = 2;
        System.out.println(multiply(first, second, scale));
    }//from   w w w.j  ava 2 s . c  o  m

    /**
     * Rounding mode.
     */
    private static final int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;

    /**
     * Multiplies first by second.
     * 
     * @param first
     *                first number to multiply
     * @param second
     *                second number to multiply
     * @param scale
     *                the number of digits to the right of the decimal point
     * @return a new BigDecimal containing the result
     */
    public static BigDecimal multiply(final BigDecimal first,
            final BigDecimal second, final int scale) {
        return first.multiply(second).setScale(scale, ROUNDING_MODE);
    }

    /**
     * Multiplies first by second.
     * 
     * @param first
     *                first number to multiply
     * @param second
     *                second number to multiply
     * @param scale
     *                the number of digits to the right of the decimal point
     * @return a new BigDecimal containing the result
     */
    public static BigDecimal multiply(final BigDecimal first,
            final int second, final int scale) {
        return first.multiply(new BigDecimal(second)).setScale(scale,
                ROUNDING_MODE);
    }

    /**
     * Multiplies first by second.
     * 
     * @param first
     *                first number to multiply
     * @param second
     *                second number to multiply
     * @param scale
     *                the number of digits to the right of the decimal point
     * @return a new BigDecimal containing the result
     */
    public static BigDecimal multiply(final int first, final int second,
            final int scale) {
        return new BigDecimal(first).multiply(new BigDecimal(second))
                .setScale(scale, ROUNDING_MODE);
    }
}

Related Tutorials