Java Number Add add(Object num1, Object num2)

Here you can find the source of add(Object num1, Object num2)

Description

add

License

Apache License

Declaration

public static Double add(Object num1, Object num2) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.math.BigDecimal;

public class Main {
    private static int DEF_SCALE = 10;

    public static Double add(Object num1, Object num2) {
        BigDecimal result = bigDecimal(num1).add(bigDecimal(num2));
        return result.setScale(DEF_SCALE, BigDecimal.ROUND_HALF_UP)
                .doubleValue();//from w  w w .j  a v  a 2s  . c  om
    }

    public static BigDecimal bigDecimal(Object object) {
        if (object == null) {
            throw new NullPointerException();
        }
        BigDecimal result;
        try {
            result = new BigDecimal(String.valueOf(object).replaceAll(",",
                    ""));
        } catch (NumberFormatException e) {
            throw new NumberFormatException("Please give me a numeral.Not "
                    + object);
        }
        return result;
    }
}

Related

  1. add(float[] param)
  2. add(long v1, long v2)
  3. add(long val, long augend)
  4. add(Number a, Number b)
  5. add(Object addend, Object augend)
  6. add(String number1, String number2)
  7. add(T a, T b)
  8. add1(Double v1, Double v2)
  9. add4Money(Double value1, Double value2)