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 boolean equalTo(BigDecimal b1, BigDecimal b2) {
    if (b1 == null || b2 == null) {
        return false;
    }//ww w .j a v  a 2  s . c o m
    return 0 == b1.compareTo(b2);
}

From source file:nl.tudelft.stocktrader.util.StockTraderUtility.java

public static BigDecimal getRandomPriceChangeFactor(BigDecimal currentPrice) {
    if (currentPrice.compareTo(PENNY_STOCK_P) == -1 || currentPrice.compareTo(PENNY_STOCK_P) == 0) {
        return JUNK_STOCK_MIRACLE_MULTIPLIER;
    } else if (currentPrice.compareTo(STOCK_P_HIGH_BAR) == 1 || currentPrice.compareTo(STOCK_P_HIGH_BAR) == 0) {
        return STOCK_P_HIGH_BAR_CRASH;
    }/* w  ww  . j a v  a2s.  c  o  m*/

    BigDecimal factor = BigDecimal.valueOf(0);
    Random rand = new Random();
    int y = rand.nextInt(STOCK_CHANGE_MAX_PERCENT.subtract(BigDecimal.ONE).intValue());
    y = y + 1;
    int x = rand.nextInt();

    if (x % 2 == 0) {
        factor = BigDecimal.ONE.subtract((BigDecimal.valueOf(y)).divide(BigDecimal.valueOf(100)));// / 100m;
    } else
        factor = BigDecimal.ONE.add(BigDecimal.ONE.add(BigDecimal.valueOf(y).divide(BigDecimal.valueOf(100))));
    return factor;
}

From source file:Main.java

public static int compareInt(double valone, double valTwo) {
    BigDecimal val1 = new BigDecimal(valone);
    BigDecimal val2 = new BigDecimal(valTwo);
    int result = -1;
    if (val1.compareTo(val2) < 0) {
        result = -1;/*from w  w  w . j  a v  a 2  s .  c om*/
    }
    if (val1.compareTo(val2) == 0) {
        result = 0;
    }
    if (val1.compareTo(val2) > 0) {
        result = 1;
    }
    return result;
}

From source file:Main.java

public static BigDecimal calculatePercent(BigDecimal numerator, BigDecimal denominator) {
    BigDecimal result = BigDecimal.ZERO;
    if (numerator != null && denominator != null && (denominator.compareTo(BigDecimal.ZERO) != 0)) {
        result = numerator.divide(denominator, 3, BigDecimal.ROUND_HALF_UP).movePointRight(2);
    }/*from   ww w.  j ava  2s  .c o  m*/
    return result;
}

From source file:Main.java

public static int compareTo(Number value1, Number value2) {
    BigDecimal b1 = new BigDecimal(Double.toString(value1.doubleValue()));
    BigDecimal b2 = new BigDecimal(Double.toString(value2.doubleValue()));
    return b1.compareTo(b2);
}

From source file:Main.java

public static BigDecimal setDecimalDigit(BigDecimal number, int digit, boolean setNullIndicator) {
    BigDecimal returnNum = BigDecimal.ZERO;
    if (number != null) {
        if ((number.compareTo(BigDecimal.ZERO) == 0) && setNullIndicator) {
            return null;
        }/*w w w  .  j av  a  2  s.c  om*/
        returnNum = number.setScale(digit, BigDecimal.ROUND_HALF_UP);
    }
    return returnNum;
}

From source file:com.excilys.ebi.bank.web.tld.Functions.java

public static String amount(BigDecimal amount) {

    StringBuilder buf = new StringBuilder();
    buf.append("<span class=\"figure ").append(amount.compareTo(BigDecimal.valueOf(0.0)) >= 0 ? "green" : "red")
            .append("\">").append(amount).append("</span>");
    return buf.toString();
}

From source file:com.nearinfinity.honeycomb.hbase.bulkload.FieldParser.java

private static ByteBuffer extractDecimal(String val, int precision, int right_scale) {
    int left_scale = precision - 2;
    BigDecimal x = new BigDecimal(val);
    boolean is_negative = x.compareTo(BigDecimal.ZERO) == -1;
    x = x.abs();//w w w.  ja va  2  s. c  om
    BigDecimal left = x.setScale(0, RoundingMode.DOWN);
    BigDecimal right = x.subtract(left).movePointRight(right_scale);
    int right_bytes_len = bytesFromDigits(right_scale);
    int left_bytes_len = bytesFromDigits(left_scale);
    byte[] left_bytes = left.toBigInteger().toByteArray();
    byte[] right_bytes = right.toBigInteger().toByteArray();
    // Bit twiddling is fun
    byte[] buff = new byte[left_bytes_len + right_bytes_len];

    System.arraycopy(left_bytes, 0, buff, left_bytes_len - left_bytes.length, left_bytes.length);
    System.arraycopy(right_bytes, 0, buff, right_bytes_len - right_bytes.length + left_bytes_len,
            right_bytes.length);

    buff[0] ^= -128; // Flip first bit, 0x80
    if (is_negative) { // Flip all bits
        for (int i = 0; i < buff.length; i++) {
            buff[i] ^= -1; // 0xff
        }
    }
    return ByteBuffer.wrap(buff);
}

From source file:Util.java

public static BigDecimal sqrt(BigDecimal number, BigDecimal guess, RoundingMode rounding) {
    BigDecimal result = BigDecimalZERO;
    BigDecimal flipA = result;/*from   w  w w  . j  a va2 s  .  com*/
    BigDecimal flipB = result;
    boolean first = true;
    while (result.compareTo(guess) != 0) {
        if (!first)
            guess = result;
        else
            first = false;

        result = number.divide(guess, rounding).add(guess).divide(BigDecimalTWO, rounding);
        // handle flip flops
        if (result.equals(flipB))
            return flipA;

        flipB = flipA;
        flipA = result;
    }
    return result;
}

From source file:com.cubeia.backoffice.accounting.core.entity.CurrencyRate.java

private static void checkRateIsNotZero(BigDecimal rate) {
    if (rate == null || rate.compareTo(new BigDecimal("0")) == 0) {
        throw new IllegalArgumentException("Rate cannot be null or zero");
    }/*ww  w  .j a va  2s  . c om*/
}