Example usage for java.math BigDecimal precision

List of usage examples for java.math BigDecimal precision

Introduction

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

Prototype

int precision

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

Click Source Link

Document

The number of decimal digits in this BigDecimal, or 0 if the number of digits are not known (lookaside information).

Usage

From source file:Main.java

public static void main(String[] args) {

    BigDecimal bg1 = new BigDecimal("123.234");
    BigDecimal bg2 = new BigDecimal("523");

    // assign the result of precision of bg1, bg2 to i1 and i2
    int i1 = bg1.precision();
    int i2 = bg2.precision();

    String str1 = "The precision of " + bg1 + " is " + i1;
    String str2 = "The precision of " + bg2 + " is " + i2;

    // print the values of i1, i2
    System.out.println(str1);//from   w ww  . j  ava  2  s .co  m
    System.out.println(str2);
}

From source file:org.cirdles.ludwig.squid25.Utilities.java

/**
 * Performs excel-style rounding of double to a given number of significant
 * figures./*from www  .  j a  v a2 s . com*/
 *
 * @param value double to round
 * @param sigFigs count of significant digits for rounding
 * @return double rounded to sigFigs significant digits
 */
public static double roundedToSize(double value, int sigFigs) {
    BigDecimal valueBDtoSize = BigDecimal.ZERO;
    if (Double.isFinite(value)) {
        BigDecimal valueBD = new BigDecimal(value);
        int newScale = sigFigs - (valueBD.precision() - valueBD.scale());
        valueBDtoSize = valueBD.setScale(newScale, RoundingMode.HALF_UP);
    }
    return valueBDtoSize.doubleValue();
}

From source file:Main.java

public static String formatAmountConversionRate(double convRate) {
    if (convRate == 0)
        return null;
    BigDecimal cr = new BigDecimal(convRate);
    int x = 7 - cr.precision() + cr.scale();
    String bds = cr.movePointRight(cr.scale()).toString();
    if (x > 9)
        bds = zeropad(bds, bds.length() + x - 9);
    String ret = zeropadRight(bds, 7);
    return Math.min(9, x) + takeFirstN(ret, 7);
}

From source file:org.osaf.cosmo.eim.schema.EimFieldValidator.java

/**
 * Validates and returns a decimal field value.
 *
 * @throws EimValidationException if the value is invalid
 *///from   w ww  . j a v  a2  s .c o m
public static BigDecimal validateDecimal(EimRecordField field, int numDigits, int numDecimalPlaces)
        throws EimValidationException {
    if (!(field instanceof DecimalField))
        throw new EimValidationException("Field " + field.getName() + " is not a decimal field");
    BigDecimal value = ((DecimalField) field).getDecimal();
    if (value == null)
        return value;
    if (numDigits > 0) {
        if (value.precision() > numDigits)
            throw new EimValidationException("Field " + field.getName() + " decimal value has "
                    + value.precision() + " digits which is more than the maximum of " + numDigits);
    }
    if (numDecimalPlaces > 0) {
        if (value.scale() > numDecimalPlaces)
            throw new EimValidationException("Field " + field.getName() + " decimal value has " + value.scale()
                    + " decimal places which is more than the maximum of " + numDecimalPlaces);
    }
    return value;
}

From source file:org.apache.carbondata.core.util.DataTypeUtil.java

/**
 * This method will check the digits before dot with the max precision allowed
 *
 * @param bigDecimal/*from w w w . ja  v  a2  s  .  c o  m*/
 * @param allowedPrecision precision configured by the user
 * @return
 */
private static BigDecimal normalizeDecimalValue(BigDecimal bigDecimal, int allowedPrecision) {
    if (bigDecimal.precision() > allowedPrecision) {
        return null;
    }
    return bigDecimal;
}

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;
    }/*from w  w w  .  j a  v  a2  s .co  m*/
    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 w  w .j  av  a 2s .  c o  m*/
    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: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.cinnober.msgcodec.json.JsonValueHandler.java

static boolean isJavaScriptSafeSigned(BigDecimal value) {
    // the number of decimal digits a double can uniquely identify is 15
    return value.precision() <= 15;
}

From source file:Main.java

public static BigDecimal log10(BigDecimal b) {
    final int NUM_OF_DIGITS = SCALE + 2;
    // need to add one to get the right number of dp
    //  and then add one again to get the next number
    //  so I can round it correctly.

    MathContext mc = new MathContext(NUM_OF_DIGITS, RoundingMode.HALF_EVEN);
    //special conditions:
    // log(-x) -> exception
    // log(1) == 0 exactly;
    // log of a number lessthan one = -log(1/x)
    if (b.signum() <= 0) {
        throw new ArithmeticException("log of a negative number! (or zero)");
    } else if (b.compareTo(BigDecimal.ONE) == 0) {
        return BigDecimal.ZERO;
    } else if (b.compareTo(BigDecimal.ONE) < 0) {
        return (log10((BigDecimal.ONE).divide(b, mc))).negate();
    }/*w w  w. jav a2  s . c  om*/

    StringBuilder sb = new StringBuilder();
    //number of digits on the left of the decimal point
    int leftDigits = b.precision() - b.scale();

    //so, the first digits of the log10 are:
    sb.append(leftDigits - 1).append(".");

    //this is the algorithm outlined in the webpage
    int n = 0;
    while (n < NUM_OF_DIGITS) {
        b = (b.movePointLeft(leftDigits - 1)).pow(10, mc);
        leftDigits = b.precision() - b.scale();
        sb.append(leftDigits - 1);
        n++;
    }

    BigDecimal ans = new BigDecimal(sb.toString());

    //Round the number to the correct number of decimal places.
    ans = ans.round(new MathContext(ans.precision() - ans.scale() + SCALE, RoundingMode.HALF_EVEN));
    return ans;
}