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:com.autentia.intra.validator.EuroValidator.java

/** */
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
    int scale = Integer.parseInt((String) component.getAttributes().get("scale"));
    int maxSize = Integer.parseInt((String) component.getAttributes().get("maxSize"));
    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"));
        }//  w w  w . jav  a 2 s. c o m

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

From source file:com.github.fge.jsonschema.keyword.digest.helpers.NumericDigester.java

protected final ObjectNode digestedNumberNode(final JsonNode schema) {
    final ObjectNode ret = FACTORY.objectNode();

    final JsonNode node = schema.get(keyword);
    final boolean isLong = valueIsLong(node);
    ret.put("valueIsLong", isLong);

    if (isLong) {
        ret.put(keyword, node.canConvertToInt() ? FACTORY.numberNode(node.intValue())
                : FACTORY.numberNode(node.longValue()));
        return ret;
    }/*from  w  w  w  .j  av  a 2s  . c om*/

    final BigDecimal decimal = node.decimalValue();
    ret.put(keyword, decimal.scale() == 0 ? FACTORY.numberNode(decimal.toBigIntegerExact()) : node);

    return ret;
}

From source file:com.autentia.intra.validator.PeriodicalAccountEntryValidator.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"));
        }/* ww w .j  ava  2 s  . c om*/

        // 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"));
        }
        PeriodicalAccountEntryBean bean = (PeriodicalAccountEntryBean) FacesUtils
                .getBean("periodicalAccountEntryBean");
        AccountEntryType type = bean.getType();
        AccountEntryGroup group = type.getGroup();
        if (group.getId() == ConfigurationUtil.getDefault().getCostId()) {
            if (bd.signum() != -1) {
                log.info("validate - value cost is negative (" + value + ")");
                throw new ValidatorException(new FacesMessage("La cantidad debe ser negativa"));
            }
        }
        if (group.getId() == ConfigurationUtil.getDefault().getIncomeId()) {
            if (bd.signum() != 1) {
                log.info("validate - value incom is positive (" + value + ")");
                throw new ValidatorException(new FacesMessage("La cantidad debe ser positiva"));
            }
        }
    }
}

From source file:com.autentia.intra.validator.AccountEntryValidator.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"));
        }/*w w w. j  av  a 2s.  c o  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"));
        }
        AccountEntryBean bean = (AccountEntryBean) FacesUtils.getBean("accountEntryBean");
        AccountEntryType type = bean.getType();
        AccountEntryGroup group = type.getGroup();

        if (group.getId() == ConfigurationUtil.getDefault().getCostId()) {
            if (bd.signum() != -1) {
                log.info("validate - value cost is negative (" + value + ")");
                throw new ValidatorException(new FacesMessage("La cantidad debe ser negativa"));
            }
        }
        if (group.getId() == ConfigurationUtil.getDefault().getIncomeId()) {
            if (bd.signum() != 1) {
                log.info("validate - value incom is positive (" + value + ")");
                throw new ValidatorException(new FacesMessage("La cantidad debe ser positiva"));
            }
        }

    }
}

From source file:org.osaf.cosmo.eim.DecimalField.java

/** */
public DecimalField(String name, BigDecimal value) {
    this(name, value, value != null ? value.precision() : -1, value != null ? value.scale() : -1);
}

From source file:com.hotelbeds.hotelapimodel.auto.convert.json.RateSerializer.java

@Override
public void serialize(BigDecimal value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
    gen.writeString(value.setScale(value.scale(), BigDecimal.ROUND_HALF_EVEN).toString());
}

From source file:py.una.pol.karaku.dao.entity.interceptors.BigDecimalInterceptor.java

@Override
public void intercept(Operation o, Field field, Object bean) {

    BigDecimal value = (BigDecimal) ReflectionUtils.getField(field, bean);
    if (value == null) {
        return;//  w  ww. jav a2  s.  co m
    }
    if (value.longValue() < 1) {
        value = value.add(BigDecimal.ONE);
    }
    BigDecimal trailed = value.stripTrailingZeros();
    int precision = trailed.scale();
    if (precision > MAXIMUM_PRECISION) {
        throw new KarakuRuntimeException(
                String.format("Attribute '%s' of bean '%s' has a precision of {%d}, maximum allowed is {%d}",
                        field.getName(), bean.getClass().getSimpleName(), precision, MAXIMUM_PRECISION));
    }
}

From source file:org.kuali.kpme.core.util.ValidationUtils.java

public static boolean validateEarnCodeFraction(String earnCode, BigDecimal amount, LocalDate asOfDate) {
    boolean valid = true;
    EarnCodeContract ec = HrServiceLocator.getEarnCodeService().getEarnCode(earnCode, asOfDate);
    if (ec != null && ec.getFractionalTimeAllowed() != null) {
        BigDecimal fracAllowed = new BigDecimal(ec.getFractionalTimeAllowed());
        if (amount == null) {
            amount = BigDecimal.ZERO;
        }//from w  w  w .  j av  a2 s.c  o  m
        if (amount.scale() > fracAllowed.scale()) {
            valid = false;
        }

    }
    return valid;
}

From source file:org.kalypso.gml.ui.internal.coverage.CoverageColorRangeAction.java

/**
 * protect against too many digits; still ugly, as number of digits depends on real data type...
 *///w w w.j  a v a 2s . c o m
private BigDecimal asBigDecimal(final Double value) {
    final BigDecimal decimal = new BigDecimal(Double.toString(value));
    if (decimal.scale() < 5)
        return decimal;

    return decimal.setScale(4, BigDecimal.ROUND_HALF_UP);
}

From source file:net.sf.jasperreports.data.cache.BigDecimalStore.java

@Override
public void addValue(Object object) {
    if (!(object instanceof BigDecimal)) {
        throw new IllegalArgumentException();
    }/*from   w ww  .  jav  a2s. c  o m*/

    BigDecimal value = (BigDecimal) object;
    BigInteger unscaledValue = value.unscaledValue();
    int scale = value.scale();

    valueStore.addValue(unscaledValue);
    scaleStore.addValue(scale);
}