Java Number Add add(final String start, final String... values)

Here you can find the source of add(final String start, final String... values)

Description

add

License

Apache License

Declaration

public static BigDecimal add(final String start, final String... values) 

Method Source Code


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

import java.math.BigDecimal;

public class Main {
    /**//from   ww  w. j av a2 s.co m
     * Add n BigDecimal safely (i.e. handles nulls)
     */
    public static BigDecimal add(final BigDecimal start, final BigDecimal... values) {
        BigDecimal total = start != null ? start : BigDecimal.ZERO;
        if (values != null) {
            for (final BigDecimal v : values) {
                total = doAdd(total, v);
            }
        }
        return total;
    }

    public static BigDecimal add(final String start, final String... values) {
        BigDecimal total = start != null ? new BigDecimal(start) : BigDecimal.ZERO;
        if (values != null) {
            for (final String v : values) {
                total = doAdd(total, new BigDecimal(v));
            }
        }
        return total;
    }

    /**
     * Add 2 BigDecimal safely (i.e. handles nulls)
     */
    private static BigDecimal doAdd(final BigDecimal v1, final BigDecimal v2) {
        BigDecimal total = v1;
        if (v1 != null && v2 != null) {
            total = v1.add(v2);
        } else if (v2 != null) {
            total = v2;
        }
        return total;
    }
}

Related

  1. add(double v1, double v2)
  2. add(double v1, double v2)
  3. add(Double... ds)
  4. add(double... ds)
  5. add(final Number a, final Number b)
  6. add(float... values)
  7. add(float[] param)
  8. add(long v1, long v2)
  9. add(long val, long augend)