java math library for BigDecimal which allows null values

你离开我真会死。 提交于 2020-07-04 21:59:32

问题


Is there a BigDecimal library with the basic operations of BigDecimal which allows null values?

Null should be treated as 0 for mathematical purpose.

I don't want to do all the null checks for possible null values.

You either never allow null values in database, application or view and initialize everything with new BigDecimal(0) or perform null checks on every usage for nullable values.

Something like:

public static BigDecimal add(final BigDecimal value, final BigDecimal augend)
{
    if (value == null)
        return augend;
    else if (augend == null)
        return value;
    else
        return value.add(augend);
}

public static BigDecimal multiply(final BigDecimal value, final BigDecimal multiplicand)
{
    if (value == null || multiplicand == null)
        return null;

    return value.multiply(multiplicand);
}

回答1:


Save the coding, just don't allow null values in the database. Make the default value zero.

As for new BigDecimal(0): no, use BigDecimal.ZERO.




回答2:


I had a similar problem (not related to a database though, just needed to sum up a couple of possibly null BigDecimals). Did not find any library, so had to write the following function myself:

public static BigDecimal add(BigDecimal... addends) {
    BigDecimal sum = BigDecimal.ZERO;
    if (addends != null) {
        for (BigDecimal addend : addends) {
            if (addend == null) {
                addend = BigDecimal.ZERO;
            }
            sum = sum.add(addend);
        }
    }
    return sum;
}

The same in Java 8:

public static BigDecimal add(BigDecimal... addends) {
    if (addends == null) {
        return BigDecimal.ZERO;
    }
    return Arrays.stream(addends)
            .filter(Objects::nonNull)
            .reduce(BigDecimal.ZERO, BigDecimal::add);
}



回答3:


I guess I don't see the point of the library checking for null. Sure, the library won't throw a NPE, but the caller is eventually going to have to check for it. What is the caller of your above multiply going to do? It can't just use the output. It's going to have to check to see if the returned value is null at some point before it can do anything with the value.

Also, for any application I've ever written, a null is much different than zero. I wouldn't want to use one that treated a null as zero.

If your requirement is that nulls aren't allowed in your DB, I would check for nulls in your DAO layer before writing to the DB.



来源:https://stackoverflow.com/questions/14823772/java-math-library-for-bigdecimal-which-allows-null-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!