mikvor / money-conversion

Money conversion and arithmetics utility classes.
64 stars 17 forks source link

add .isZero() comparison #13

Open alensiljak opened 9 years ago

alensiljak commented 9 years ago

Hi,

Checking whether an amount is zero, negative, or positive is quite a common usage scenario and adding helper methods would make this check much easier. Currently we have to create a zero object and compare the actual value with this. JavaMoney has some great comparators, like .isZero(), isZeroOrNegative(), etc. (https://github.com/JavaMoney/jsr354-ri/blob/master/src/main/java/org/javamoney/moneta/FastMoney.java) Would be great if Money type had something similar built in.

i.e.

@Override
    public boolean isZero() {
        return this.number == 0L;
    }

    @Override
    public boolean isPositive() {
        return this.number > 0L;
    }

    @Override
    public boolean isPositiveOrZero() {
        return this.number >= 0L;
    }

    @Override
    public boolean isNegative() {
        return this.number < 0L;
    }

    @Override
    public boolean isNegativeOrZero() {
        return this.number <= 0L;
    }

BigDecimal implementation is using .signum(). https://github.com/JavaMoney/jsr354-ri/blob/master/src/main/java/org/javamoney/moneta/Money.java

ratcashdev commented 6 years ago

You may want to consider using SIGNUM. More details here: https://github.com/MisterY/money-conversion/commit/97dabbf34786e8d17a05b90cfd490e36058751d5#commitcomment-29308125