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:com.feilong.core.lang.NumberUtil.java

/**
 * <code>one/two</code>,?? <code>roundingMode</code> ? ?? <code>scale</code>.
 * //from w w  w.j a v  a  2s  .  co m
 * <h3>:</h3>
 * 
 * <blockquote>
 * 
 * <pre class="code">
 * NumberUtil.getDivideValue(0, 2, 0,RoundingMode.HALF_UP)   =   0
 * NumberUtil.getDivideValue(6, 4, 0,RoundingMode.HALF_UP)   =   2
 * NumberUtil.getDivideValue(10, 3, 2,RoundingMode.HALF_UP)  =   3.33
 * NumberUtil.getDivideValue(5, 3, 2,RoundingMode.HALF_UP)   =   1.67
 * </pre>
 * 
 * </blockquote>
 * 
 * @param one
 *            
 * @param two
 *            ,?{@link BigDecimal}??
 * @param scale
 *            ,??,see {@link java.math.BigDecimal#setScale(int, RoundingMode)}
 * @param roundingMode
 *            ? {@link RoundingMode}
 * @return  <code>one</code>  null, {@link NullPointerException}<br>
 *          <code>two</code>  null, {@link NullPointerException}<br>
 *          <code>two</code>  0, {@link IllegalArgumentException}<br>
 *         ???{@link BigDecimal} one/two,??? <code>roundingMode</code>,?? <code>scale</code> 
 * @see <a href="#RoundingMode">JAVA 8??</a>
 * @see java.math.BigDecimal#divide(BigDecimal, int, RoundingMode)
 * @since 1.5.5
 */
public static BigDecimal getDivideValue(Number one, Number two, int scale, RoundingMode roundingMode) {
    Validate.notNull(one, "one can't be null!");
    Validate.notNull(two, "two can't be null!");

    BigDecimal divisor = toBigDecimal(two);
    Validate.isTrue(!divisor.equals(new BigDecimal(0)), "two can't be zero!");

    // ?one.divide(two),scaleroundingMode,?????.
    // ?? exception:Non-terminating decimal expansion; no exact representable decimal result
    return toBigDecimal(one).divide(divisor, scale, roundingMode);
}

From source file:org.mcisb.util.math.MathUtils.java

/**
 * /*from   ww  w . j av a  2s .  c  om*/
 * @param values
 * @param start
 * @param end
 * @param binSize
 * @return int[]
 */
public static int[] toHistogram(final double[] values, final double start, final double end,
        final double binSize) {
    final BigDecimal remainder = BigDecimal.valueOf(end - start).remainder(BigDecimal.valueOf(binSize));

    if (remainder.equals(BigDecimal.ZERO)) {
        throw new IllegalArgumentException("Invalid bin size for range of " + (end - start)); //$NON-NLS-1$
    }

    Arrays.sort(values);

    if (values[0] < start) {
        throw new IllegalArgumentException("Value " + values[0] + " out of range"); //$NON-NLS-1$ //$NON-NLS-2$
    }
    if (values[values.length - 1] > end) {
        throw new IllegalArgumentException("Value " + values[values.length - 1] + " out of range"); //$NON-NLS-1$ //$NON-NLS-2$
    }

    final int numBins = (int) ((end - start) / binSize);
    final int[] bins = new int[numBins];

    for (double value : values) {
        final int index = BigDecimal.valueOf(value).equals(BigDecimal.valueOf(end)) ? numBins - 1
                : (int) ((value - start) / binSize);
        bins[index]++;
    }

    return bins;
}

From source file:com.huateng.startup.init.MenuInfoUtil.java

private static void valueIdRole(List<Object[]> funcMapList) {
    List<Object[]> list = new ArrayList<Object[]>();

    for (int i = 0; i < funcMapList.size(); i++) {
        Object[] obj = funcMapList.get(i);
        BigDecimal valueId = (BigDecimal) obj[1];
        String roleFlag = (String) obj[5];

        for (int j = i + 1; j < funcMapList.size(); j++) {
            Object[] obj2 = funcMapList.get(j);
            BigDecimal valueId2 = (BigDecimal) obj2[1];
            String roleFlag2 = (String) obj2[5];
            if (valueId.equals(valueId2)) {
                roleFlag = roleFlag + roleFlag2;
                list.add(obj2);//from w w w  .  j a v  a2  s  .co m
            }
        }
        obj[5] = roleFlag;
    }
    for (Object[] ov : list) {
        funcMapList.remove(ov);
    }
}

From source file:adalid.commons.util.ObjUtils.java

public static BigDecimal reciprocal(Object o) {
    BigDecimal pdq = NumUtils.numberToBigDecimal(o);
    return pdq == null || pdq.equals(BigDecimal.ZERO) ? null : BigDecimal.ONE.divide(pdq);
}

From source file:com.wms.utils.DataUtil.java

/**
 * Kiem tra Bigdecimal bi null hoac zero
 *
 * @param value/*from w w  w.ja v a  2s.com*/
 * @return
 */
public static boolean isNullOrZero(BigDecimal value) {
    return (value == null || value.equals(BigDecimal.ZERO));
}

From source file:org.kalypso.ui.wizards.results.ResultSldHelper.java

public static IStatus processStyle(final IFile styleFile, final IFolder sldFolder, final String type,
        final BigDecimal minValue, final BigDecimal maxValue) {
    /* Read sld from template */

    // better, get the sld from resource folder
    final URL resource = ResultSldHelper.class.getResource("resources/myDefault" + type + ".sld"); //$NON-NLS-1$ //$NON-NLS-2$
    try {/*from www  .j av a 2  s. c o  m*/
        final StyledLayerDescriptor sld = SLDFactory.createSLD(resource);

        if (NodeResultHelper.LINE_TYPE.equals(type) || NodeResultHelper.POLYGON_TYPE.equals(type)) {
            //resolve the problem with initial creation of style files in case with min equals max
            BigDecimal lMax = maxValue;
            if (minValue.equals(maxValue)) {
                lMax = maxValue.add(new BigDecimal(1));
            }
            processTinStyle(type, minValue, lMax, sld);
            /* Write SLD back to file */
            if (sld != null) {
                final String sldXML = sld.exportAsXML();
                final String sldXMLwithHeader = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + sldXML; //$NON-NLS-1$
                styleFile.create(new StringInputStream(sldXMLwithHeader, "UTF-8"), false, //$NON-NLS-1$
                        new NullProgressMonitor());
            }
        } else if (NodeResultHelper.NODE_TYPE.equals(type)) {
            // we creating more styles for nodes to give the possibility for representation of different types of node
            // results
            createDefaultNodeStyles(sldFolder, maxValue, resource, type);
            // String sldXMLwithHeader = processVectorStyle( maxValue, resource, type ); styleFile,
            // styleFile.create( new StringInputStream( sldXMLwithHeader ), false, new NullProgressMonitor() );
        }
    } catch (final Exception e) {
        e.printStackTrace();
        return StatusUtilities.statusFromThrowable(e,
                Messages.getString("org.kalypso.ui.wizards.results.ResultSldHelper.0")); //$NON-NLS-1$
    }
    return Status.OK_STATUS;

}

From source file:com.github.jessemull.microflex.stat.statbigdecimal.GeometricMeanBigDecimalTest.java

/**
 * Corrects any rounding errors due to differences in the implementation of
 * the statistic between the Apache and MicroFlex libraries
 * @param    BigDecimal    the first result
 * @param    BigDecimal    the second result
 * @return                 corrected results
 */// www .ja  v  a 2  s  .c  o  m
private static BigDecimal[] correctRoundingErrors(BigDecimal bd1, BigDecimal bd2) {

    BigDecimal[] array = new BigDecimal[2];
    int scale = mc.getPrecision();

    while (!bd1.equals(bd2) && scale > mc.getPrecision() / 4) {

        bd1 = bd1.setScale(scale, RoundingMode.HALF_DOWN);
        bd2 = bd2.setScale(scale, RoundingMode.HALF_DOWN);

        if (bd1.subtract(bd1.ulp()).equals(bd2)) {
            bd1 = bd1.subtract(bd1.ulp());
        }

        if (bd1.add(bd1.ulp()).equals(bd2)) {
            bd1 = bd1.add(bd1.ulp());
        }

        scale--;
    }

    array[0] = bd1;
    array[1] = bd2;

    return array;
}

From source file:com.xeiam.xchange.bitstamp.dto.marketdata.BitstampTransaction.java

private BigDecimal roundUp(BigDecimal x) {

    long n = x.longValue();
    return new BigDecimal(x.equals(new BigDecimal(n)) ? n : n + 1);
}

From source file:org.stockwatcher.domain.WatchListItem.java

public double getRateOfReturn() {
    if (startPrice == null || startPrice.equals(ZERO)) {
        throw new IllegalStateException("startPrice is null or zero");
    }/*ww  w  .j a  v a  2  s  . c  om*/
    BigDecimal currentPrice = getCurrentPrice();
    if (currentPrice == null || currentPrice.equals(ZERO)) {
        throw new IllegalStateException("currentPrice is null or zero");
    }
    return currentPrice.subtract(startPrice).doubleValue() / startPrice.doubleValue();
}

From source file:com.sun.faces.el.impl.Coercions.java

/**
 * Performs all of the necessary type conversions, then calls on the
 * appropriate operator./*from   w w w .j ava2s .  co m*/
 */
public static Object applyEqualityOperator(Object pLeft, Object pRight, EqualityOperator pOperator)
        throws ElException {
    if (pLeft == pRight) {
        return PrimitiveObjects.getBoolean(pOperator.apply(true));
    } else if (pLeft == null || pRight == null) {
        return PrimitiveObjects.getBoolean(pOperator.apply(false));
    } else if (isBigDecimal(pLeft) || isBigDecimal(pRight)) {
        BigDecimal left = (BigDecimal) coerceToPrimitiveNumber(pLeft, BigDecimal.class);
        BigDecimal right = (BigDecimal) coerceToPrimitiveNumber(pRight, BigDecimal.class);
        return PrimitiveObjects.getBoolean(pOperator.apply(left.equals(right)));
    } else if (isFloatingPointType(pLeft) || isFloatingPointType(pRight)) {
        double left = coerceToPrimitiveNumber(pLeft, Double.class).doubleValue();
        double right = coerceToPrimitiveNumber(pRight, Double.class).doubleValue();
        return PrimitiveObjects.getBoolean(pOperator.apply(left == right));
    } else if (isBigInteger(pLeft) || isBigInteger(pRight)) {
        BigInteger left = (BigInteger) coerceToPrimitiveNumber(pLeft, BigInteger.class);
        BigInteger right = (BigInteger) coerceToPrimitiveNumber(pRight, BigInteger.class);
        return PrimitiveObjects.getBoolean(pOperator.apply(left.equals(right)));
    } else if (isIntegerType(pLeft) || isIntegerType(pRight)) {
        long left = coerceToPrimitiveNumber(pLeft, Long.class).longValue();
        long right = coerceToPrimitiveNumber(pRight, Long.class).longValue();
        return PrimitiveObjects.getBoolean(pOperator.apply(left == right));
    } else if (pLeft instanceof Boolean || pRight instanceof Boolean) {
        boolean left = coerceToBoolean(pLeft).booleanValue();
        boolean right = coerceToBoolean(pRight).booleanValue();
        return PrimitiveObjects.getBoolean(pOperator.apply(left == right));
    } else if (pLeft instanceof String || pRight instanceof String) {
        String left = coerceToString(pLeft);
        String right = coerceToString(pRight);
        return PrimitiveObjects.getBoolean(pOperator.apply(left.equals(right)));
    } else {
        try {
            return PrimitiveObjects.getBoolean(pOperator.apply(pLeft.equals(pRight)));
        } catch (Exception exc) {
            if (log.isErrorEnabled()) {
                String message = MessageUtil.getMessageWithArgs(Constants.ERROR_IN_EQUALS,
                        pLeft.getClass().getName(), pRight.getClass().getName(), pOperator.getOperatorSymbol());
                log.error(message, exc);
                throw new ElException(message, exc);
            }
            return Boolean.FALSE;
        }
    }
}