Java Number Add add(double v1, double v2)

Here you can find the source of add(double v1, double v2)

Description

Addition for two double value

License

Open Source License

Parameter

Parameter Description
v1 <code>double</code>
v2 <code>double</code>

Return

addition result

Declaration

public static double add(double v1, double v2) 

Method Source Code


//package com.java2s;
/*/*from  www .  j a  va  2 s.  co m*/
 * File: $RCSfile$
 *
 * Copyright (c) 2005 Wincor Nixdorf International GmbH,
 * Heinz-Nixdorf-Ring 1, 33106 Paderborn, Germany
 * All Rights Reserved.
 *
 * This software is the confidential and proprietary information
 * of Wincor Nixdorf ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered
 * into with Wincor Nixdorf.
 */

import java.math.BigDecimal;

public class Main {
    /**
     * Addition for two double value
     *
     * @param v1 <code>double</code>
     * @param v2 <code>double</code>
     * @return addition result
     */
    public static double add(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.add(b2).doubleValue();
    }

    /**
     * Addition for two integer value
     *
     * @param v1 <code>Integer</code>
     * @param v2 <code>Integer</code>
     * @return addition result
     */
    public static Integer add(Integer v1, Integer v2) {
        if (v1 == null && v2 == null) {
            return null;
        }
        if (v1 == null) {
            v1 = new Integer(0);
        }
        if (v2 == null) {
            v2 = new Integer(0);
        }
        return new Integer(v1.intValue() + v2.intValue());
    }

    /**
     * Addition for two <code>java.math.BigDecimal</code> value
     *
     * @param v1 <code>java.math.BigDecimal</code>
     * @param v2 <code>java.math.BigDecimal</code>
     * @return addition result
     */
    public static BigDecimal add(BigDecimal v1, BigDecimal v2) {
        if (v1 == null && v2 == null) {
            return null;
        }
        if (v1 == null) {
            v1 = new BigDecimal(0);
        }
        if (v2 == null) {
            v2 = new BigDecimal(0);
        }
        return v1.add(v2);
    }
}

Related

  1. add(Double a, Double b)
  2. add(double v1, double v2)
  3. add(Double v1, Double v2)
  4. add(double v1, double v2)
  5. add(Double v1, Double v2)
  6. add(double v1, double v2)
  7. add(Double v1, Double v2)
  8. add(double... ds)
  9. add(Double... ds)