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:org.kalypso.grid.AsciiRandomAccessGeoGrid.java

private void readRow(final int y) throws IOException {
    m_randomAccessFile.seek(m_rowPositions[y]);
    final String line = m_randomAccessFile.readLine();

    try (final Scanner scanner = new Scanner(line)) {
        for (int x = 0; x < m_sizeX; x++) {
            final String next = scanner.next();
            final BigDecimal value = new BigDecimal(next);
            m_rowData[x] = value.equals(m_noDataValue) ? Double.NaN : value.doubleValue();
        }//from   w w  w  . j a  v a 2 s  . c o m
    }

    m_currentRow = y;
}

From source file:pyromaniac.Algorithm.QuinceOUFrequencyTable.java

public void loadProbabilities(String probFile) {
    try {// w  w  w .j av a2 s . c o  m
        URL url = getClass().getResource(probFile);
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

        String header = in.readLine();

        if (header.split(",").length != 4) {
            throw new Exception("Call probabilities does not have four columns as expected");
        }

        String line = in.readLine();

        while (line != null) {
            String[] fields = line.split(",");

            int mode = Integer.parseInt(fields[0]);
            BigDecimal lessThan = new BigDecimal(fields[1]).setScale(SCALE, BigDecimal.ROUND_HALF_UP);
            BigDecimal equalTo = new BigDecimal(fields[2]).setScale(SCALE, BigDecimal.ROUND_HALF_UP);
            BigDecimal moreThan = new BigDecimal(fields[3]).setScale(SCALE, BigDecimal.ROUND_HALF_UP);

            BigDecimal sum = lessThan.add(equalTo).add(moreThan).setScale(SCALE, BigDecimal.ROUND_HALF_UP);

            if (!sum.equals(new BigDecimal("1").setScale(SCALE, BigDecimal.ROUND_HALF_UP))) {
                lessThan = lessThan.divide(sum, SCALE, BigDecimal.ROUND_HALF_UP);
                equalTo = equalTo.divide(sum, SCALE, BigDecimal.ROUND_HALF_UP);
                moreThan = moreThan.divide(sum, SCALE, BigDecimal.ROUND_HALF_UP);
            }

            BigDecimal[] probs = { lessThan, equalTo, moreThan };
            this.probabilities.put(mode, probs);
            line = in.readLine();
        }
    } catch (NumberFormatException nfe) {
        nfe.printStackTrace();
    } catch (IOException ie) {
        ie.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:pyromaniac.Algorithm.QuinceOUFrequencyTable.java

private BigDecimal[] _calculateProbabilitiesHelper(int mode) {
    BigDecimal sd = new BigDecimal("0.04").add(new BigDecimal(mode).multiply(new BigDecimal("0.03")));
    BigDecimal modeBD = new BigDecimal(mode);

    BigDecimal lowerBound = modeBD.subtract(new BigDecimal(SUBTRACT_FOR_LB)).setScale(SCALE,
            BigDecimal.ROUND_HALF_UP);
    BigDecimal upperBound = modeBD.add(new BigDecimal(ADD_FOR_UB)).setScale(SCALE, BigDecimal.ROUND_HALF_UP);

    NormalDistribution norm = new NormalDistribution(mode, sd.doubleValue());

    try {/*from  w  w w. j  av a2s .  c  o  m*/
        BigDecimal probLessThan = new BigDecimal(norm.cumulativeProbability(lowerBound.doubleValue()))
                .setScale(SCALE, BigDecimal.ROUND_HALF_UP);
        BigDecimal probMoreThan = new BigDecimal("1")
                .subtract(new BigDecimal(norm.cumulativeProbability(upperBound.doubleValue())))
                .setScale(SCALE, BigDecimal.ROUND_HALF_UP);
        BigDecimal probEqualTo = new BigDecimal("1").subtract(probLessThan).subtract(probMoreThan)
                .setScale(SCALE, BigDecimal.ROUND_HALF_UP);

        BigDecimal totalProb = probLessThan.add(probEqualTo).add(probMoreThan).setScale(SCALE,
                BigDecimal.ROUND_HALF_UP);

        if (!totalProb.equals(new BigDecimal("1").setScale(SCALE, BigDecimal.ROUND_HALF_UP))) {
            probLessThan = probLessThan.divide(totalProb, SCALE, BigDecimal.ROUND_HALF_UP);
            probMoreThan = probMoreThan.divide(totalProb, SCALE, BigDecimal.ROUND_HALF_UP);
            probEqualTo = probEqualTo.divide(totalProb, SCALE, BigDecimal.ROUND_HALF_UP);
        }

        BigDecimal[] probs = { probLessThan, probEqualTo, probMoreThan };
        return probs;
    } catch (MathIllegalStateException me) {
        me.getStackTrace();
    }
    return null;
}

From source file:test.Test_User.java

public void xx1() throws SQLException {
    System.out.println(File.separator);

    PreparedStatement ps = cpds.getConnection()
            .prepareStatement("select amount from fc_transaction where id = 12");

    BigDecimal db = new BigDecimal("15.465482652446425");

    ResultSet rs = ps.executeQuery();
    if (rs.next()) {
        BigDecimal b = rs.getBigDecimal(1);
        System.out.println(b.equals(db) + "\n" + b);
    }/*from www.  j  a  v a2 s.  c  om*/
}

From source file:org.jspringbot.keyword.json.JSONHelper.java

public void jsonValueShouldBe(String jsonExpression, String expectedValue) {
    Object jsonValue = getJsonValue(jsonExpression);

    if (Number.class.isInstance(jsonValue)) {
        BigDecimal expectedNumberValue = new BigDecimal(expectedValue);
        BigDecimal jsonNumberValue = new BigDecimal(jsonValue.toString());
        if (!expectedNumberValue.equals(jsonNumberValue)) {
            throw new IllegalArgumentException(String.format("Expecting '%s' json number value but was '%s'",
                    expectedValue, String.valueOf(jsonValue)));
        }//from w  w w .j  a  v a  2  s.  c  o m
    } else {
        if (!expectedValue.equals(String.valueOf(jsonValue))) {
            throw new IllegalArgumentException(String.format("Expecting '%s' json string value but was '%s'",
                    expectedValue, String.valueOf(jsonValue)));
        }
    }
}

From source file:org.castor.cpa.test.test12.TestTypeConversion.java

public void testValuesInDB() throws Exception {
    LOG.debug("Test values in database");
    // Create a statement and a resultset
    _db.begin();//  w ww .jav a 2s . c o  m
    Connection conn = _db.getJdbcConnection();
    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery("select bool_int, " + "bool_int_minus, bool_bigdec, bool_bigdec_minus "
            + "from test12_conv where id = " + TypeConversion.DEFAULT_ID);
    rs.next();
    BigDecimal bigPlus = rs.getBigDecimal("bool_bigdec");
    BigDecimal bigMinus = rs.getBigDecimal("bool_bigdec_minus");
    if ((rs.getInt("bool_int") != 1) && (rs.getInt("bool_int_minus") != -1)
            && !bigPlus.equals(new BigDecimal(1)) && !bigMinus.equals(new BigDecimal(-1))) {

        LOG.error(REASON);
        fail(REASON);
    }
    _db.commit();
    LOG.debug("OK: Found the expected Values in database");
}

From source file:org.libreplan.web.orders.HoursGroupWrapper.java

private BigDecimal getPercentageInOrderLineGroup(Integer workHours) {
    BigDecimal workingHours = new BigDecimal(hoursGroup.getWorkingHours()).setScale(2);
    BigDecimal total = new BigDecimal(workHours).setScale(2);

    return total.equals(new BigDecimal(0).setScale(2)) ? new BigDecimal(0).setScale(2)
            : workingHours.divide(total, BigDecimal.ROUND_DOWN).scaleByPowerOfTen(2);
}

From source file:pyromaniac.Algorithm.BalzerOUCallFrequencyTable.java

/**
 * _calculate probability.//  w ww  .  j a  v  a2s  .c  om
 *
 * @param mode the mode
 * @param segmentNumber the segment number
 * @return the BigDecimal [] of probabilities, all with scale of 10.
 */
private BigDecimal[] _calculateProbabilitiesHelper(int segmentNumber, int mode) {
    BigDecimal sd, mean, modeBD;

    //this multiplicative factor was taken from elsewhere...
    BigDecimal flowEffect = new BigDecimal("0.003").multiply(new BigDecimal(segmentNumber)).setScale(SCALE,
            BigDecimal.ROUND_HALF_UP);

    modeBD = new BigDecimal(mode);

    if (mode >= 6) {
        mean = new BigDecimal(mode).setScale(SCALE, BigDecimal.ROUND_HALF_UP);
        //standard deviation is 0.03 + effect of RefLen + effect of flow position 
        sd = new BigDecimal("0.03494").add(mean.multiply(new BigDecimal("0.06856"))).add(flowEffect);
    } else {
        mean = new BigDecimal(this.normalDistParams.get(mode).getFirst()).setScale(SCALE,
                BigDecimal.ROUND_HALF_UP);
        sd = new BigDecimal(this.normalDistParams.get(mode).getSecond()).add(flowEffect).setScale(SCALE,
                BigDecimal.ROUND_HALF_UP);
    }

    NormalDistribution norm = new NormalDistribution(mean.doubleValue(), sd.doubleValue());

    try {
        //due to rounding...
        //cumulative probability [X <= x]
        //so prob under is [X <= MODE - 0.51], and prob over is 1 - prob [X <= MODE + 0.49] (i.e. prob X > MODE + 0.49)
        BigDecimal lowerBound = modeBD.subtract(new BigDecimal(SUBTRACT_FOR_LB)).setScale(SCALE,
                BigDecimal.ROUND_HALF_UP);
        BigDecimal upperBound = modeBD.add(new BigDecimal(ADD_FOR_UB)).setScale(SCALE,
                BigDecimal.ROUND_HALF_UP);

        BigDecimal probLessThan = new BigDecimal(norm.cumulativeProbability(lowerBound.doubleValue()))
                .setScale(SCALE, BigDecimal.ROUND_HALF_UP);
        BigDecimal probMoreThan = new BigDecimal("1")
                .subtract(new BigDecimal(norm.cumulativeProbability(upperBound.doubleValue())).setScale(SCALE,
                        BigDecimal.ROUND_HALF_UP));
        BigDecimal probEqualTo = new BigDecimal("1").subtract(probLessThan).subtract(probMoreThan)
                .setScale(SCALE, BigDecimal.ROUND_HALF_UP);

        BigDecimal summed = probLessThan.add(probEqualTo).add(probMoreThan).setScale(SCALE,
                BigDecimal.ROUND_HALF_UP);
        if (!summed.equals(new BigDecimal("1").setScale(SCALE, BigDecimal.ROUND_HALF_UP))) {
            probLessThan = probLessThan.divide(summed, SCALE, BigDecimal.ROUND_HALF_UP);
            probMoreThan = probMoreThan.divide(summed, SCALE, BigDecimal.ROUND_HALF_UP);
            probEqualTo = probEqualTo.divide(summed, SCALE, BigDecimal.ROUND_HALF_UP);
        }

        BigDecimal[] probs = { probLessThan, probEqualTo, probMoreThan };

        return probs;
    } catch (MathIllegalStateException me) {
        me.getStackTrace();
    }
    return null;
}

From source file:au.org.ala.delta.translation.key.KeyStateTranslator.java

/**
 * Produces a state description of a key state defined for a numeric character.
 * @param keyChar the character as defined by the KEY STATES directive.
 * @param state details of the redefined state to describe.
 * @param separator the separator to use.
 * @return a description of the supplied state of the supplied character.
 *//*w  w w .  j a v a  2s  .co  m*/
private String translateNumericState(IdentificationKeyCharacter keyChar, NumericKeyState state) {

    BigDecimal min = state.min();
    BigDecimal max = state.max();
    List<String> states = new ArrayList<String>();

    String separator = " " + Words.word(Word.TO) + " ";
    Values values = new Values(states, separator);

    if (keyChar.getCharacter().getCharacterType().isNumeric()) {

        if (min.equals(MIN_VALUE)) {
            values.setPrefix(Words.word(Word.UP_TO));
            states.add(max.toPlainString());
        } else if (max.equals(MAX_VALUE)) {
            values.setSuffix(Words.word(Word.OR_MORE));
            states.add(min.toPlainString());
        } else {
            states.add(min.toPlainString());
            if (min.compareTo(max) != 0) {
                states.add(max.toPlainString());
            }
        }
    }

    AttributeTranslator at = _attributeTranslatorFactory.translatorFor(keyChar.getCharacter());

    StringBuffer result = new StringBuffer();
    if (StringUtils.isNotBlank((values.getPrefix()))) {
        result.append(values.getPrefix()).append(" ");
    }

    result.append(at.translateValues(values));

    if (StringUtils.isNotBlank(values.getSuffix())) {
        result.append(" ").append(values.getSuffix());
    }
    return result.toString();
}