absolute value Compare To BigDecimal - Java java.math

Java examples for java.math:BigDecimal Compare

Description

absolute value Compare To BigDecimal

Demo Code


//package com.java2s;
import java.math.BigDecimal;

public class Main {
    public static void main(String[] argv) throws Exception {
        BigDecimal v1 = new BigDecimal("1234");
        BigDecimal v2 = new BigDecimal("1234");
        System.out.println(absCompareTo(v1, v2));
    }/*from  ww  w  .j  av a 2  s.  c  om*/

    /**
     * @return true if the ABS(v1) > ABS(v2)
     */
    public static int absCompareTo(final BigDecimal v1, final BigDecimal v2) {
        return compareTo(abs(v1), abs(v2));
    }

    /**
     * @return 1 if v1 > v2 or v2==null and v2!=null
     * @return 0 if v1 == v2 or v1==null and v2==null
     * @return -1 if v1 < v2 or v1==null and v2!=null
     */
    public static int compareTo(final BigDecimal v1, final BigDecimal v2) {
        int ret = 1;
        if (v1 != null && v2 != null) {
            ret = v1.compareTo(v2);
        } else if (v1 == null && v2 == null) {
            ret = 0;
        } else if (v1 == null) {
            ret = -1;
        }
        return ret;
    }

    /**
     * Returns the ABS of the value, handles null.
     */
    public static BigDecimal abs(final BigDecimal value) {
        return value != null ? value.abs() : null;
    }
}

Related Tutorials