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:de.jfachwert.math.Bruch.java

private static Bruch toBruch(BigDecimal decimal) {
    int scale = decimal.scale();
    BigInteger z = decimal.movePointRight(scale).toBigInteger();
    BigInteger n = BigDecimal.ONE.movePointRight(scale).toBigInteger();
    return Bruch.of(z, n).kuerzen();
}

From source file:org.goko.core.rs274ngcv3.RS274.java

public static BigDecimal buildBigDecimal(String value) throws GkException {
    BigDecimal bigDecimal = new BigDecimal(value);
    if (RS274Preference.getInstance().isDecimalTruncateEnabled()) {
        int decimalCount = RS274Preference.getInstance().getDecimalCount();
        if (bigDecimal.scale() > decimalCount) {
            bigDecimal = bigDecimal.setScale(decimalCount, RoundingMode.DOWN);
        }/*from  w  w  w .j a  v a  2 s  .  c  o m*/
    }
    return bigDecimal;
}

From source file:NumberUtil.java

/**
 * Returns the BigDecimal value n with trailing
 * zeroes removed./*w w w.  java 2 s.  c  o m*/
 */
public static BigDecimal trim(BigDecimal n) {
    try {
        while (true) {
            n = n.setScale(n.scale() - 1);
        }
    } catch (ArithmeticException e) {
        // no more trailing zeroes so exit.
    }
    return n;
}

From source file:org.eel.kitchen.jsonschema.GsonProvider.java

private static JsonNode toNumberNode(final BigDecimal decimal) {
    try {//from w w w .  j  a  v  a 2 s . c  o  m
        return factory.numberNode(decimal.intValueExact());
    } catch (ArithmeticException ignored) {
        try {
            return factory.numberNode(decimal.longValueExact());
        } catch (ArithmeticException ignoredAgain) {
            return decimal.scale() == 0 ? factory.numberNode(decimal.toBigInteger())
                    : factory.numberNode(decimal);
        }
    }
}

From source file:org.apache.carbondata.core.scan.executor.util.RestructureUtil.java

/**
 * Method for computing measure default value based on the data type
 *
 * @param columnSchema/*  w  ww .j a  va2 s. co  m*/
 * @param defaultValue
 * @return
 */
public static Object getMeasureDefaultValue(ColumnSchema columnSchema, byte[] defaultValue) {
    Object measureDefaultValue = null;
    if (!isDefaultValueNull(defaultValue)) {
        String value = null;
        switch (columnSchema.getDataType()) {
        case SHORT:
        case INT:
        case LONG:
            value = new String(defaultValue, Charset.forName(CarbonCommonConstants.DEFAULT_CHARSET));
            measureDefaultValue = Long.parseLong(value);
            break;
        case DECIMAL:
            BigDecimal decimal = DataTypeUtil.byteToBigDecimal(defaultValue);
            if (columnSchema.getScale() > decimal.scale()) {
                decimal = decimal.setScale(columnSchema.getScale(), RoundingMode.HALF_UP);
            }
            measureDefaultValue = decimal;
            break;
        default:
            value = new String(defaultValue, Charset.forName(CarbonCommonConstants.DEFAULT_CHARSET));
            Double parsedValue = Double.valueOf(value);
            if (!Double.isInfinite(parsedValue) && !Double.isNaN(parsedValue)) {
                measureDefaultValue = parsedValue;
            }
        }
    }
    return measureDefaultValue;
}

From source file:org.apache.carbondata.core.scan.executor.util.RestructureUtil.java

/**
 * Gets the default value based on the column data type.
 *
 * @param columnSchema//w  w  w. j a va2  s  . co  m
 * @param defaultValue
 * @return
 */
public static Object getMeasureDefaultValueByType(ColumnSchema columnSchema, byte[] defaultValue) {
    Object measureDefaultValue = null;
    if (!isDefaultValueNull(defaultValue)) {
        String value = null;
        switch (columnSchema.getDataType()) {
        case SHORT:
            value = new String(defaultValue, Charset.forName(CarbonCommonConstants.DEFAULT_CHARSET));
            measureDefaultValue = Short.parseShort(value);
            break;
        case INT:
            value = new String(defaultValue, Charset.forName(CarbonCommonConstants.DEFAULT_CHARSET));
            measureDefaultValue = Integer.parseInt(value);
            break;
        case LONG:
            value = new String(defaultValue, Charset.forName(CarbonCommonConstants.DEFAULT_CHARSET));
            measureDefaultValue = Long.parseLong(value);
            break;
        case DECIMAL:
            BigDecimal decimal = DataTypeUtil.byteToBigDecimal(defaultValue);
            if (columnSchema.getScale() > decimal.scale()) {
                decimal = decimal.setScale(columnSchema.getScale(), RoundingMode.HALF_UP);
            }
            measureDefaultValue = Decimal.apply(decimal);
            break;
        default:
            value = new String(defaultValue, Charset.forName(CarbonCommonConstants.DEFAULT_CHARSET));
            Double parsedValue = Double.valueOf(value);
            if (!Double.isInfinite(parsedValue) && !Double.isNaN(parsedValue)) {
                measureDefaultValue = parsedValue;
            }
        }
    }
    return measureDefaultValue;
}

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

/**
 * This method will convert a big decimal value to bytes
 *
 * @param num//  w  w  w .  j a  v a 2s  .  co m
 * @return
 */
public static byte[] bigDecimalToByte(BigDecimal num) {
    BigInteger sig = new BigInteger(num.unscaledValue().toString());
    int scale = num.scale();
    byte[] bscale = new byte[] { (byte) (scale) };
    byte[] buff = sig.toByteArray();
    byte[] completeArr = new byte[buff.length + bscale.length];
    System.arraycopy(bscale, 0, completeArr, 0, bscale.length);
    System.arraycopy(buff, 0, completeArr, bscale.length, buff.length);
    return completeArr;
}

From source file:Main.java

/**
 * Compute the natural logarithm of x to a given scale, x > 0.
 *//*from  ww w  .j a va 2 s  .c om*/
public static BigDecimal ln(BigDecimal x, int scale) {
    // Check that x > 0.
    if (x.signum() <= 0) {
        throw new IllegalArgumentException("x <= 0");
    }

    // The number of digits to the left of the decimal point.
    int magnitude = x.toString().length() - x.scale() - 1;

    if (magnitude < 3) {
        return lnNewton(x, scale);
    }

    // Compute magnitude*ln(x^(1/magnitude)).
    else {

        // x^(1/magnitude)
        BigDecimal root = intRoot(x, magnitude, scale);

        // ln(x^(1/magnitude))
        BigDecimal lnRoot = lnNewton(root, scale);

        // magnitude*ln(x^(1/magnitude))
        return BigDecimal.valueOf(magnitude).multiply(lnRoot).setScale(scale, BigDecimal.ROUND_HALF_EVEN);
    }
}

From source file:com.owncloud.android.utils.DisplayUtils.java

/**
 * Converts the file size in bytes to human readable output.
 * <ul>/*  ww  w . jav a 2 s .  c  om*/
 *     <li>appends a size suffix, e.g. B, KB, MB etc.</li>
 *     <li>rounds the size based on the suffix to 0,1 or 2 decimals</li>
 * </ul>
 *
 * @param bytes Input file size
 * @return Like something readable like "12 MB"
 */
public static String bytesToHumanReadable(long bytes, Context context) {
    if (bytes < 0) {
        return context.getString(R.string.common_pending);

    } else {
        double result = bytes;
        int attachedSuff = 0;
        while (result >= 1024 && attachedSuff < sizeSuffixes.length) {
            result /= 1024.;
            attachedSuff++;
        }

        BigDecimal readableResult = new BigDecimal(result)
                .setScale(sizeScales[attachedSuff], BigDecimal.ROUND_HALF_UP).stripTrailingZeros();

        // Unscale only values with ten exponent
        return (readableResult.scale() < 0 ? readableResult.setScale(0) : readableResult) + " "
                + sizeSuffixes[attachedSuff];
    }
}

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

/**
 * Validates and returns a decimal field value.
 *
 * @throws EimValidationException if the value is invalid
 */// w  ww  . j  a va2 s  .com
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;
}