Example usage for java.math BigDecimal compareTo

List of usage examples for java.math BigDecimal compareTo

Introduction

In this page you can find the example usage for java.math BigDecimal compareTo.

Prototype

@Override
public int compareTo(BigDecimal val) 

Source Link

Document

Compares this BigDecimal with the specified BigDecimal .

Usage

From source file:Main.java

public static void main(String[] args) {

    BigDecimal bg1 = new BigDecimal("10");
    BigDecimal bg2 = new BigDecimal("20");

    int res = bg1.compareTo(bg2); // compare bg1 with bg2

    String str1 = "Both values are equal ";
    String str2 = "First Value is greater ";
    String str3 = "Second value is greater";

    if (res == 0)
        System.out.println(str1);
    else if (res == 1)
        System.out.println(str2);
    else if (res == -1)
        System.out.println(str3);
}

From source file:Main.java

public static BigDecimal max(final List<BigDecimal> list) {
    BigDecimal max = BigDecimal.ZERO;
    for (final BigDecimal item : list) {
        if (item.compareTo(max) > 0)
            max = item;//  w  w  w .  j  a va  2s  .c o  m
    }
    return max;
}

From source file:Main.java

public static BigDecimal calculateDiscountAmt(BigDecimal Discount, BigDecimal DiscountAmt) {
    if (DiscountAmt == null || Discount.compareTo(BigDecimal.ZERO) == 0)
        return BigDecimal.ZERO;
    else {/*  w  w w  .j  a v  a 2  s  .  c om*/
        BigDecimal discountPerc = Discount.divide(new BigDecimal(100));
        return DiscountAmt.multiply(discountPerc);
    }
}

From source file:Main.java

public static boolean displayedAsScientific(Object number) {
    BigDecimal num = getBigDecimal(number);
    if (num.compareTo(MAX_POSITIVE_DECIMAL_NUMBER) <= 0 && num.compareTo(MIN_POSITIVE_DECIMAL_NUMBER) >= 0) {
        return false;
    }//from  www  .  j a  v a2  s .  co  m
    if (num.compareTo(MAX_NEGATIVE_DECIMAL_NUMBER) <= 0 && num.compareTo(MIN_NEGATIVE_DECIMAL_NUMBER) >= 0) {
        return false;
    }
    return true;
}

From source file:Main.java

public static boolean compare(BigDecimal val1, BigDecimal val2) {
    boolean result = false;
    if (val1.compareTo(val2) < 0) {
        result = false;/*w  w  w . jav a2s  .c om*/
    }
    if (val1.compareTo(val2) == 0) {
        result = true;
    }
    if (val1.compareTo(val2) > 0) {
        result = true;
    }
    return result;
}

From source file:Main.java

public static int compareTo(double v1, double v2) {
    BigDecimal b1 = new BigDecimal(v1);
    BigDecimal b2 = new BigDecimal(v2);
    return b1.compareTo(b2);
}

From source file:Main.java

public static boolean isBigNumber(Object number) {
    if (number == null) {
        return false;
    }//from ww w  . ja  va 2 s.  c  o  m
    try {
        BigDecimal num = getBigDecimal(number);
        if (num.compareTo(MAX_DOUBLE) == 1 || num.compareTo(MIN_DOUBLE) == -1) {
            return true;
        } else {
            return false;
        }
    } catch (Exception e) {
        return false;
    }
}

From source file:Main.java

public static int compareTo(String arg0, String arg1) {
    BigDecimal b1 = new BigDecimal(arg0);
    BigDecimal b2 = new BigDecimal(arg1);
    return b1.compareTo(b2);
}

From source file:Main.java

public static BigDecimal calculateDivide(BigDecimal numerator, BigDecimal denominator) {
    BigDecimal result = BigDecimal.ZERO;
    if (denominator.compareTo(BigDecimal.ZERO) != 0) {
        result = numerator.divide(denominator, 3, BigDecimal.ROUND_HALF_UP);
    }//  ww w  . j ava  2  s  .  co  m
    return result;
}

From source file:Main.java

public static boolean compare(String v1, String v2) {
    BigDecimal b1 = new BigDecimal(v1);
    BigDecimal b2 = new BigDecimal(v2);
    int bj = b1.compareTo(b2);
    boolean res;//from  ww  w . java  2s. c o  m
    if (bj > 0)
        res = true;
    else
        res = false;
    return res;
}