Example usage for java.math BigDecimal equals

List of usage examples for java.math BigDecimal equals

Introduction

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

Prototype

@Override
public boolean equals(Object x) 

Source Link

Document

Compares this BigDecimal with the specified Object for equality.

Usage

From source file:Main.java

public static void main(String[] args) {

    BigDecimal bg1 = new BigDecimal("25.00");
    BigDecimal bg2 = new BigDecimal("25.00");
    BigDecimal bg3 = new BigDecimal("25");

    // assign the result of equals method to b1, b2
    Boolean b1 = bg1.equals(bg2);
    Boolean b2 = bg1.equals(bg3);

    System.out.println(b1);/*from  w ww  . j a va  2 s.  c  o  m*/
    System.out.println(b2);
}

From source file:MainClass.java

public static void main(String argv[]) {
    BigDecimal first = new BigDecimal("3419229223372036854775807.23343");
    BigDecimal second = new BigDecimal("2.0");
    System.out.println(first.add(second));
    System.out.println(first.subtract(second));
    System.out.println(first.divide(second));
    System.out.println(first.equals(second));
    System.out.println(first.abs());
    System.out.println(first.max(second));
    System.out.println(first.min(second));
    System.out.println(first.remainder(second));
}

From source file:Main.java

/**
 * Divides two decimals and applies the given scale and a ROUND_HALF_UP. This method should be used only for the final result
 * calculation. For example if we have something like this: (axb)/c the rules should be applied to the result of the division
 * only and not all the computations that give us the final result.
 * /*  ww w .ja  va 2s .com*/
 * @param dividend the dividend
 * @param divisor the divisor
 * @param scale the scale for the result
 * @return the result of the division after the scale and rounding are applied
 */
public static BigDecimal divide(BigDecimal dividend, BigDecimal divisor, int scale) {

    BigDecimal result = BigDecimal.ZERO;

    if (dividend != null && divisor != null && !divisor.equals(BigDecimal.ZERO)) {
        result = dividend.divide(divisor, scale, BigDecimal.ROUND_HALF_UP);
    }

    return result;
}

From source file:org.apache.fineract.portfolio.servicecharge.util.ServiceChargeOperationUtils.java

public static BigDecimal divideNonZeroValues(BigDecimal operand, BigDecimal divisor) {
    if (operand == null) {
        return BigDecimal.ONE;
    }//from www.ja v a  2  s. com
    if (divisor != null && !divisor.equals(BigDecimal.ZERO)) {
        operand = operand.divide(divisor, RoundingMode.HALF_UP);
    }
    return operand;
}

From source file:ws.moor.bt.util.ByteUtil.java

public static int distance(byte[] a, byte[] b) {
    byte[] result = xor(a, b);
    if (result.length == 0) {
        result = new byte[] { 1 };
    }//from  ww  w.j ava2 s.co  m
    result = ArrayUtil.append(new byte[] { 0 }, result);
    BigDecimal number = new BigDecimal(new BigInteger(result));
    if (number.equals(BigDecimal.ZERO)) {
        number = BigDecimal.ONE;
    }
    double clear = BigDecimal.valueOf(2).pow(a.length * 8).divide(number, 4, RoundingMode.UP).doubleValue();
    return (int) (100.0 * Math.log(clear) / Math.log(2.0) / a.length);
}

From source file:org.apache.fineract.portfolio.servicecharge.util.ServiceChargeOperationUtils.java

public static BigDecimal divideAndMultiplyNonZeroValues(BigDecimal operand, BigDecimal divisor,
        BigDecimal multiplicand) {
    if (operand == null) {
        return BigDecimal.ONE;
    }/*w  w w .j a v a2 s .  co  m*/
    if (divisor != null && !divisor.equals(BigDecimal.ZERO)) {
        operand = operand.divide(divisor, RoundingMode.HALF_UP);
    }
    if (multiplicand != null) {
        operand = operand.multiply(multiplicand);
    }
    return operand;
}

From source file:com.SCI.centraltoko.utility.UtilityTools.java

public static String formatNumber(BigDecimal value) {
    if (value == null || value.equals(BigDecimal.ZERO)) {
        return "0";
    } else {//ww  w  .j  a v  a  2 s. c om
        NumberFormat formater = NumberFormat.getInstance();
        formater.setMaximumFractionDigits(0);
        formater.setMinimumFractionDigits(0);
        return formater.format(value.setScale(0, RoundingMode.HALF_EVEN));
    }
}

From source file:org.openmrs.module.openhmis.cashier.api.util.RoundingUtil.java

/**
 * Add a rounding line item to a bill if necessary
 * @param bill//  w w  w  .ja v a2s .c  o m
 * @should handle a rounding line item (add/update/delete with the appropriate value)
 * @should not modify a bill that needs no rounding
 * @should round bills with a non zero amount correctly for MID
 * @should round bills with a non zero amount correctly for CEILING
 * @should round bills with a non zero amount correctly for FLOOR
 */
public static void handleRoundingLineItem(Bill bill) {
    ICashierOptionsService cashOptService = Context.getService(ICashierOptionsService.class);
    CashierOptions options = cashOptService.getOptions();
    if (options.getRoundToNearest().equals(BigDecimal.ZERO)) {
        return;
    }

    if (options.getRoundingItemUuid() == null) {
        throw new APIException(
                "No rounding item specified in options. This must be set in order to use rounding for bill totals.");
    }

    // Get rounding item
    IItemDataService itemService = Context.getService(IItemDataService.class);
    Item roundingItem = itemService.getByUuid(options.getRoundingItemUuid());

    BillLineItem roundingLineItem = findRoundingLineItem(bill, roundingItem);

    BigDecimal difference = calculateRoundingValue(bill, options, roundingLineItem);

    if (difference.equals(BigDecimal.ZERO) && roundingLineItem != null) {
        bill.removeLineItem(roundingLineItem);
    } else if (!difference.equals(BigDecimal.ZERO) && roundingLineItem == null) {
        // Create line item for rounding item and the required amount
        bill.addLineItem(roundingItem, difference.abs(), "",
                difference.compareTo(BigDecimal.ZERO) > 0 ? -1 : 1);
    } else if (!difference.equals(BigDecimal.ZERO)) {
        updateRoundingItem(bill, difference, roundingLineItem);
    }
    bill.recalculateLineItemOrder();
}

From source file:universal.Calc.java

public static BigDecimal sqrt(BigDecimal A, final int SCALE) {
    BigDecimal x0 = new BigDecimal("0");
    BigDecimal x1 = new BigDecimal(Math.sqrt(A.doubleValue()));
    while (!x0.equals(x1)) {
        x0 = x1;/*w ww.  j  a v  a  2 s  .c  o  m*/
        x1 = A.divide(x0, SCALE, RoundingMode.HALF_UP);
        x1 = x1.add(x0);
        x1 = x1.divide(new BigDecimal(2), SCALE, RoundingMode.HALF_UP);
    }
    return x1;
}

From source file:Util.java

public static BigDecimal sqrt(BigDecimal number, BigDecimal guess, RoundingMode rounding) {
    BigDecimal result = BigDecimalZERO;
    BigDecimal flipA = result;/*from  w  ww .  j  ava2s .c o m*/
    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;
}