Example usage for java.math BigDecimal scale

List of usage examples for java.math BigDecimal scale

Introduction

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

Prototype

int scale

To view the source code for java.math BigDecimal scale.

Click Source Link

Document

The scale of this BigDecimal, as returned by #scale .

Usage

From source file:org.kalypso.model.wspm.tuhh.core.wspwin.WspWinImporter.java

/**
 * Returns the station with the most significant decimals.
 *///from   ww  w .  ja v a  2 s  . com
private static BigDecimal fixStation(final BigDecimal profStation, final BigDecimal beanStation) {
    final int profScale = profStation.scale();
    final int beanScale = beanStation.scale();

    if (profScale > beanScale)
        return profStation;
    else
        return beanStation;
}

From source file:ch.algotrader.option.OptionSymbol.java

/**
 * Generates the ISIN for the specified {@link ch.algotrader.entity.security.OptionFamily}.
 */// w ww  . j a v  a  2 s .  c  om
public static String getIsin(OptionFamily family, LocalDate expiration, OptionType type, BigDecimal strike) {

    String week = family.isWeekly() ? DateTimePatterns.WEEK_OF_MONTH.format(expiration) : "";

    String month;
    if (OptionType.CALL.equals(type)) {
        month = monthCallEnc[expiration.getMonthValue() - 1];
    } else {
        month = monthPutEnc[expiration.getMonthValue() - 1];
    }

    int yearIndex = expiration.getYear() % 10;
    String year = yearEnc[yearIndex];

    String strike36 = BaseConverterUtil.toBase36(strike.multiply(new BigDecimal(10)).intValue());
    String strikeVal = strike.scale() + StringUtils.leftPad(strike36, 4, "0");

    StringBuilder buffer = new StringBuilder();
    buffer.append("1O");
    buffer.append(family.getIsinRoot() != null ? family.getIsinRoot() : family.getSymbolRoot());
    buffer.append(week);
    buffer.append(month);
    buffer.append(year);
    buffer.append(strikeVal);

    return buffer.toString();
}

From source file:org.sonar.db.AbstractDbTester.java

private static List<Map<String, Object>> getHashMap(ResultSet resultSet) throws Exception {
    ResultSetMetaData metaData = resultSet.getMetaData();
    int colCount = metaData.getColumnCount();
    List<Map<String, Object>> rows = newArrayList();
    while (resultSet.next()) {
        Map<String, Object> columns = newHashMap();
        for (int i = 1; i <= colCount; i++) {
            Object value = resultSet.getObject(i);
            if (value instanceof Clob) {
                Clob clob = (Clob) value;
                value = IOUtils.toString((clob.getAsciiStream()));
                doClobFree(clob);/*from  w w w. j ava2  s  .c  o m*/
            } else if (value instanceof BigDecimal) {
                // In Oracle, INTEGER types are mapped as BigDecimal
                BigDecimal bgValue = ((BigDecimal) value);
                if (bgValue.scale() == 0) {
                    value = bgValue.longValue();
                } else {
                    value = bgValue.doubleValue();
                }
            } else if (value instanceof Integer) {
                // To be consistent, all INTEGER types are mapped as Long
                value = ((Integer) value).longValue();
            } else if (value instanceof Timestamp) {
                value = new Date(((Timestamp) value).getTime());
            }
            columns.put(metaData.getColumnLabel(i), value);
        }
        rows.add(columns);
    }
    return rows;
}

From source file:com.prowidesoftware.swift.utils.SwiftFormatUtils.java

/**
 * Return the number of decimals for the given number, which can be <code>null</code>, in which case this method returns zero.
 * //from  ww  w  .  j a v  a  2s  .co m
 * @return the number of decimal in the number or zero if there are none or the amount is <code>null</code>
 * @since 7.8
 */
public static int decimalsInAmount(final BigDecimal amount) {
    if (amount != null) {
        BigDecimal d = new BigDecimal(amount.toString());
        BigDecimal result = d.subtract(d.setScale(0, RoundingMode.FLOOR)).movePointRight(d.scale());
        if (result.intValue() != 0) {
            return result.toString().length();
        }
    }
    return 0;
}

From source file:org.psikeds.resolutionengine.datalayer.knowledgebase.util.FeatureValueHelper.java

public static List<FloatFeatureValue> calculateFloatRange(final String featureID, final String rangeID,
        final BigDecimal min, final BigDecimal max, final BigDecimal inc, int scale, final int roundingMode,
        final int maxSize) {
    float finc = (inc == null ? 0.0f : inc.floatValue());
    if (finc == 0.0f) {
        finc = DEFAULT_RANGE_STEP;//www.j  a v a  2 s  .com
    } else if (scale == FloatFeatureValue.MIN_FLOAT_SCALE) {
        // scale is based on the increment, if not explicitly specified otherwise
        scale = inc.scale();
    }
    final float fmin = (min == null ? 0.0f : min.floatValue());
    final float fmax = (max == null ? 0.0f : max.floatValue());
    if ((finc > 0.0f) && (fmax < fmin)) {
        throw new IllegalArgumentException(
                "Maximum of Range " + rangeID + " must not be smaller than Minimum!");
    }
    if ((finc < 0.0f) && (fmax > fmin)) {
        throw new IllegalArgumentException(
                "Minimum of Range " + rangeID + " must not be smaller than Maximum!");
    }
    final List<FloatFeatureValue> lst = new ArrayList<FloatFeatureValue>();
    int count = 1;
    for (float f = fmin; (f <= fmax) && (count <= maxSize); f = f + finc) {
        final String featureValueID = rangeID + "_F" + String.valueOf(count);
        final FloatFeatureValue ffv = new FloatFeatureValue(featureID, featureValueID, f, scale, roundingMode);
        lst.add(ffv);
        count++;
    }
    return lst;
}

From source file:net.tradelib.misc.StrategyText.java

static private String formatOrderPrice(BigDecimal bd) {
    bd = bd.setScale(7, RoundingMode.HALF_UP).stripTrailingZeros();
    int scale = bd.scale() <= 2 ? 2 : bd.scale();
    return String.format("%." + Integer.toString(scale) + "f", bd);
}

From source file:net.tradelib.misc.StrategyText.java

static private String formatBigDecimal(BigDecimal bd, int minPrecision, int maxPrecision) {
    bd = bd.setScale(maxPrecision, RoundingMode.HALF_UP).stripTrailingZeros();
    int scale = bd.scale() <= minPrecision ? minPrecision : bd.scale();
    return String.format("%,." + Integer.toString(scale) + "f", bd);
}

From source file:jp.co.ctc_g.jse.core.validation.util.Validators.java

protected static boolean isDecimal(BigDecimal number, boolean signed, int precision, int scale) {
    if ((!signed) && (number.signum() < 0)) {
        return false;
    }/*  www . ja  v  a  2  s  .  com*/
    int expectedIntPrecision = precision - scale;
    int actualPrecision = number.precision();
    int actualScale = number.scale();
    int actualIntPrecision = actualPrecision - actualScale;
    return (expectedIntPrecision >= actualIntPrecision && scale >= actualScale);
}

From source file:org.voltdb.regressionsuites.RegressionSuite.java

protected static final BigDecimal roundDecimalValue(String decimalValueString, boolean roundingEnabled,
        RoundingMode mode) {/*from w  ww . jav a  2 s. c  om*/
    BigDecimal bd = new BigDecimal(decimalValueString);
    if (!roundingEnabled) {
        return bd;
    }
    int precision = bd.precision();
    int scale = bd.scale();
    int lostScale = scale - m_defaultScale;
    if (lostScale <= 0) {
        return bd;
    }
    int newPrecision = precision - lostScale;
    MathContext mc = new MathContext(newPrecision, mode);
    BigDecimal nbd = bd.round(mc);
    assertTrue(nbd.scale() <= m_defaultScale);
    if (nbd.scale() != m_defaultScale) {
        nbd = nbd.setScale(m_defaultScale);
    }
    assertEquals(getRoundingString("Decimal Scale setting failure"), m_defaultScale, nbd.scale());
    return nbd;
}

From source file:com.autentia.intra.validator.AccountEntryValidatorOSE.java

/** */
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
    log.info("validate - value = " + value);
    if (value != null) {
        // Check if value is a BigDecimal
        if (!(value instanceof BigDecimal)) {
            log.info("validate - value is not a BigDecimal (" + value.getClass().getName() + ")");
            throw new ValidatorException(
                    new FacesMessage("Las cantidades monetarias deben ser de tipo BigDecimal"));
        }//from  w w w .  j a  v a 2 s  . co  m

        // Check if it has no more than 2 decimal digits
        BigDecimal bd = (BigDecimal) value;
        if (bd.scale() > 2) {
            log.info("validate - value has more than 2 decimals (" + value + ")");
            throw new ValidatorException(
                    new FacesMessage("Las cantidades monetarias no pueden tener mas de dos decimales"));
        }

    }
}