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.kuali.kpme.tklm.leave.donation.validation.LeaveDonationValidation.java

private boolean validateFraction(String earnCode, BigDecimal amount, LocalDate asOfDate, String fieldName) {
    boolean valid = true;
    if (!ValidationUtils.validateEarnCodeFraction(earnCode, amount, asOfDate)) {
        EarnCodeContract ec = HrServiceLocator.getEarnCodeService().getEarnCode(earnCode, asOfDate);
        if (ec != null && ec.getFractionalTimeAllowed() != null) {
            BigDecimal fracAllowed = new BigDecimal(ec.getFractionalTimeAllowed());
            String[] parameters = new String[2];
            parameters[0] = earnCode;//from  w  w w.j av a 2  s .  c  om
            parameters[1] = Integer.toString(fracAllowed.scale());
            this.putFieldError(fieldName, "error.amount.fraction", parameters);
            valid = false;
        }
    }
    return valid;
}

From source file:au.org.ala.delta.model.attribute.SignificantFiguresAttributeChunkFormatter.java

/**
 * Determines the precision to format the number to.
 * @param number the number to determine.
 * @return the precision (number of figures) to format the supplied number to.
 *///from w  w  w.j av a2 s  . c  om
private int determinePrecision(BigDecimal number) {

    // Adjust the precision because CONFOR doesn't strictly do significant figures - it only applies them
    // to values after the decimal place.
    // e.g. 123456.78 to 5 significant figures should be 123460 but CONFOR will output 123456

    int digitsToLeftOfPoint = number.precision() - number.scale();
    return Math.max(NUM_SIGNIFICANT_FIGURES, digitsToLeftOfPoint);

}

From source file:org.kuali.kpme.tklm.leave.adjustment.validation.LeaveAdjustmentValidation.java

private boolean validateFraction(String earnCode, BigDecimal amount, LocalDate asOfDate) {
    boolean valid = true;
    if (!ValidationUtils.validateEarnCodeFraction(earnCode, amount, asOfDate)) {
        EarnCodeContract ec = HrServiceLocator.getEarnCodeService().getEarnCode(earnCode, asOfDate);
        if (ec != null && ec.getFractionalTimeAllowed() != null) {
            BigDecimal fracAllowed = new BigDecimal(ec.getFractionalTimeAllowed());
            String[] parameters = new String[2];
            parameters[0] = earnCode;/*from  w w w  . ja v a2  s .  c  o  m*/
            parameters[1] = Integer.toString(fracAllowed.scale());
            this.putFieldError("adjustmentAmount", "error.amount.fraction", parameters);
            valid = false;
        }
    }
    return valid;
}

From source file:com.aoindustries.website.clientarea.accounting.MakePaymentStoredCardForm.java

@Override
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
    ActionErrors errors = new ActionErrors();
    if (GenericValidator.isBlankOrNull(paymentAmount)) {
        errors.add("paymentAmount", new ActionMessage("makePaymentStoredCardForm.paymentAmount.required"));
    } else {//from w ww.j  a v  a 2 s  . com
        try {
            BigDecimal pa = new BigDecimal(this.paymentAmount);
            if (pa.compareTo(BigDecimal.ZERO) <= 0) {
                errors.add("paymentAmount",
                        new ActionMessage("makePaymentStoredCardForm.paymentAmount.mustBeGeaterThanZero"));
            } else if (pa.scale() > 2) {
                // Must not have more than 2 decimal places
                errors.add("paymentAmount",
                        new ActionMessage("makePaymentStoredCardForm.paymentAmount.invalid"));
            }
        } catch (NumberFormatException err) {
            errors.add("paymentAmount", new ActionMessage("makePaymentStoredCardForm.paymentAmount.invalid"));
        }
    }
    return errors;
}

From source file:org.trend.hgraph.VariousTest.java

@Test
public void testBigDecimalCompare() {
    BigDecimal a = null;
    BigDecimal b = null;//from  w w  w.  j  a v a  2s.  c om

    System.out.println("***test scale");
    a = new BigDecimal("2.12345");
    System.out.println(a.scale());

    a = new BigDecimal("200.12345");
    System.out.println(a.scale());

    a = new BigDecimal("200.12");
    System.out.println(a.scale());

    a = new BigDecimal("200.12345");
    System.out.println(a);
    a = a.setScale(2, RoundingMode.DOWN);
    System.out.println(a);
    System.out.println(a.scale());

    System.out.println("***test compare #1");
    a = new BigDecimal("500.123456");
    b = new BigDecimal("500.126543");

    System.out.println("a=" + a);
    System.out.println("b=" + b);
    System.out.println("a comapre b =>" + a.compareTo(b));

    a = a.setScale(2, RoundingMode.DOWN);
    b = b.setScale(2, RoundingMode.DOWN);

    System.out.println("a=" + a);
    System.out.println("b=" + b);
    System.out.println("a comapre b =>" + a.compareTo(b));

    System.out.println("***test compare #2");
    a = new BigDecimal("500.1");
    b = new BigDecimal("500.126543");

    System.out.println("a=" + a);
    System.out.println("b=" + b);
    System.out.println("a comapre b =>" + a.compareTo(b));

    a = a.setScale(3, RoundingMode.DOWN);
    b = b.setScale(3, RoundingMode.DOWN);

    System.out.println("a=" + a);
    System.out.println("b=" + b);
    System.out.println("a comapre b =>" + a.compareTo(b));
}

From source file:com.aoindustries.website.clientarea.accounting.MakePaymentNewCardForm.java

@Override
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
    ActionErrors errors = super.validate(mapping, request);
    if (errors == null)
        errors = new ActionErrors();
    if (GenericValidator.isBlankOrNull(paymentAmount)) {
        errors.add("paymentAmount", new ActionMessage("makePaymentStoredCardForm.paymentAmount.required"));
    } else {//from w ww  . j a  v  a 2  s . c  o m
        try {
            // Make sure can parse as int-of-pennies format (Once we no longer use int-of-pennies, this should be removed)
            // Long-term plan is to use BigDecimal exclusively for all monetary values. - DRA 2007-10-09
            int pennies = SQLUtility.getPennies(this.paymentAmount);
            // Make sure can parse as BigDecimal, and is correct value
            BigDecimal pa = new BigDecimal(this.paymentAmount);
            if (pa.compareTo(BigDecimal.ZERO) <= 0) {
                errors.add("paymentAmount",
                        new ActionMessage("makePaymentStoredCardForm.paymentAmount.mustBeGeaterThanZero"));
            } else if (pa.scale() > 2) {
                // Must not have more than 2 decimal places
                errors.add("paymentAmount",
                        new ActionMessage("makePaymentStoredCardForm.paymentAmount.invalid"));
            }
        } catch (NumberFormatException err) {
            errors.add("paymentAmount", new ActionMessage("makePaymentStoredCardForm.paymentAmount.invalid"));
        }
    }
    return errors;
}

From source file:org.opensingular.form.wicket.mapper.DecimalMapper.java

private String formatDecimal(BigDecimal bigDecimal, boolean groupingUsed) {
    DecimalFormat nf = (DecimalFormat) DecimalFormat.getInstance(new Locale("pt", "BR"));
    nf.setParseBigDecimal(true);/*from   w  w w  . ja v  a  2  s . c o m*/
    nf.setGroupingUsed(groupingUsed);
    nf.setMinimumFractionDigits(0);
    nf.setMaximumFractionDigits(bigDecimal.scale());
    return nf.format(bigDecimal);
}

From source file:org.apache.hadoop.hive.serde2.io.BigDecimalWritable.java

public void set(BigDecimal value) {
    value = value.stripTrailingZeros();//from  w  ww. ja  va 2  s . c  o  m
    if (value.compareTo(BigDecimal.ZERO) == 0) {
        // Special case for 0, because java doesn't strip zeros correctly on that number.
        value = BigDecimal.ZERO;
    }
    set(value.unscaledValue().toByteArray(), value.scale());
}

From source file:ccc.cli.StringToDecConverterUtil.java

/**
 *
 * Converts a number represented as text to a decimal number.
 *
 * @param textNumber text representation of a number
 * @return BigDecimal//from w  ww .j  a v a 2s .c  o m
 */
public BigDecimal convert(final String textNumber) {
    BigDecimal decNumber = null;
    if (textNumber != null && textNumber.matches(DECIMAL_PATTERN)) {
        // BigNumber(String) constructor does not allow commas
        decNumber = new BigDecimal(textNumber.replaceAll(",", ""));

        if (decNumber.precision() > MAX_PRECISION
                || decNumber.scale() < 0
                        && Math.abs(decNumber.scale()) + decNumber.precision() > MAX_PRECISION - MAX_SCALE
                || decNumber.compareTo(MAX_VALUE) == 1) {

            LOG.warn("Value is larger than maximum precision allowed " + "and was rejected.");
            return null;
        }

        if (decNumber.scale() > MAX_SCALE) {
            LOG.trace("Scale is bigger than maximum allowed: " + decNumber.scale() + ". Resetting scale.");
            decNumber = decNumber.setScale(MAX_SCALE, RoundingMode.DOWN);
        }
    }
    return decNumber;
}

From source file:org.kuali.rice.core.api.util.type.AbstractKualiDecimal.java

/**
 * Wraps BigDecimal's add method to accept and return T instances instead of
 * BigDecimals, so that users of the class don't have to typecast the return
 * value.//from  ww w.j a  v a2  s  . c om
 * 
 * @param addend
 * @return result of adding the given addend to this value
 * @throws IllegalArgumentException
 *             if the given addend is null
 */
public T add(T addend) {
    if (addend == null) {
        throw new IllegalArgumentException("invalid (null) addend");
    }

    BigDecimal sum = this.value.add(addend.value);
    return newInstance(sum, sum.scale());
}