Provides accurate multiplication among double value. - Java java.lang

Java examples for java.lang:double

Description

Provides accurate multiplication among double value.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        double v1 = 2.45678;
        double v2 = 2.45678;
        System.out.println(mul(v1, v2));
    }//from w ww. j a  v a  2  s .c o  m

    /**
     * Provides accurate multiplication.
     *
     * @param v1
     * @param v2
     * @return
     */
    public static double mul(double v1, double v2) {
        java.math.BigDecimal b1 = new java.math.BigDecimal(
                Double.toString(v1));
        java.math.BigDecimal b2 = new java.math.BigDecimal(
                Double.toString(v2));
        return b1.multiply(b2).doubleValue();
    }

    /**
     * Provides accurate multiplication.
     *
     * @param v1
     * @param v2
     * @return
     */
    public static double mul(String v1, String v2) {
        java.math.BigDecimal b1 = new java.math.BigDecimal(v1);
        java.math.BigDecimal b2 = new java.math.BigDecimal(v2);
        return b1.multiply(b2).doubleValue();
    }
}

Related Tutorials