Java Number Add addDoubles(String str1, String str2, int rounding)

Here you can find the source of addDoubles(String str1, String str2, int rounding)

Description

add two double strings

License

Apache License

Parameter

Parameter Description
str1 a parameter
str2 a parameter
rounding a parameter

Declaration

public static String addDoubles(String str1, String str2, int rounding) 

Method Source Code

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

import java.math.BigDecimal;

public class Main {
    /**/*from  w  w w  .j  a  v a  2s  . c om*/
     * add two double strings 
     * @param str1
     * @param str2
     * @param rounding
     * @return
     */
    public static String addDoubles(String str1, String str2, int rounding) {
        double d1 = toDouble(str1);
        double d2 = toDouble(str2);
        double rc = round(d1 + d2, rounding);
        return "" + rc;
    }

    /**
     * add two double strings and rounds to 2 decimals 
     * @param str1
     * @param str2
     * @return
     */
    public static String addDoubles(String str1, String str2) {
        return addDoubles(str1, str2, 2);
    }

    /**
     * toDouble 
     */
    public static double toDouble(String str) {
        double dval = 0.0;
        if (str == null || str.equalsIgnoreCase("null") || str.equalsIgnoreCase("") || str.equalsIgnoreCase("0"))
            return 0.00;
        // double
        dval = Double.parseDouble(str.trim());
        return dval;
    }

    /**
     * round
     * 
     * @param a
     * @param b
     * @return
     */
    public static double round(double value, int decimalPlace) {
        BigDecimal bd = new BigDecimal(value);
        bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
        return bd.doubleValue();
    }
}

Related

  1. add(T a, T b)
  2. add1(Double v1, Double v2)
  3. add4Money(Double value1, Double value2)
  4. addAmounts(final double num1, final double num2)
  5. addDoubles(Double value, Double addValue)
  6. DoubleToInt(double d, double multiply, double add)
  7. readDoubleAsString(String doubleAsString)