Example usage for java.math BigDecimal stripTrailingZeros

List of usage examples for java.math BigDecimal stripTrailingZeros

Introduction

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

Prototype

public BigDecimal stripTrailingZeros() 

Source Link

Document

Returns a BigDecimal which is numerically equal to this one but with any trailing zeros removed from the representation.

Usage

From source file:Main.java

public static void main(String[] args) {

    BigDecimal bg1 = new BigDecimal("235.000");
    BigDecimal bg2 = new BigDecimal("23500");

    // assign the result of stripTrailingZeros method to bg3, bg4
    BigDecimal bg3 = bg1.stripTrailingZeros();
    BigDecimal bg4 = bg2.stripTrailingZeros();

    String str1 = bg1 + " after removing trailing zeros " + bg3;
    String str2 = bg2 + " after removing trailing zeros " + bg4;

    // print bg3, bg4 values
    System.out.println(str1);// w  ww .  java2 s .com
    System.out.println(str2);
}

From source file:ch.algotrader.util.RoundUtil.java

public static int getDigits(BigDecimal value) {
    String string = value.stripTrailingZeros().toPlainString();
    int index = string.indexOf(".");
    int scale = index < 0 ? 0 : string.length() - index - 1;
    return scale;
}

From source file:Main.java

private static javax.xml.datatype.Duration normaliseSeconds(javax.xml.datatype.Duration duration) {
    BigInteger years = (BigInteger) duration.getField(DatatypeConstants.YEARS);
    BigInteger months = (BigInteger) duration.getField(DatatypeConstants.MONTHS);
    BigInteger days = (BigInteger) duration.getField(DatatypeConstants.DAYS);

    BigInteger hours = (BigInteger) duration.getField(DatatypeConstants.HOURS);
    BigInteger minutes = (BigInteger) duration.getField(DatatypeConstants.MINUTES);
    BigDecimal seconds = (BigDecimal) duration.getField(DatatypeConstants.SECONDS);

    seconds = seconds.stripTrailingZeros();

    boolean positive = duration.getSign() >= 0;

    return FACTORY.newDuration(positive, years, months, days, hours, minutes, seconds);
}

From source file:com.willetinc.hadoop.mapreduce.dynamodb.BinarySplitter.java

/**
 * Return the string encoded in a BigDecimal. Repeatedly multiply the input
 * value by 16; the integer portion after such a multiplication represents a
 * single character in base 16. Convert that back into a char and create a
 * string out of these until we have no data left.
 * //from   ww  w  . j  a v  a 2 s .c om
 * @throws IOException
 */
static byte[] bigDecimalToByteArray(BigDecimal bd, int maxBytes) {
    BigDecimal cur = bd.stripTrailingZeros();
    ByteArrayOutputStream sb = new ByteArrayOutputStream();

    try {
        byte[] curCodePoint = new byte[1];
        for (int numConverted = 0; numConverted < maxBytes; numConverted++) {
            cur = cur.multiply(ONE_PLACE);
            curCodePoint[0] = cur.byteValue();
            if (0x0 == curCodePoint[0]) {
                break;
            }

            cur = cur.subtract(new BigDecimal(new BigInteger(curCodePoint)));
            sb.write(curCodePoint);
        }
    } catch (IOException e) {
        LOG.error("Error writing byte array", e);
    }

    return sb.toByteArray();
}

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

/**
 * Overrides formatNumber in the parent class to format the number to 5 significant figures.  Trailing
 * zeros are stripped./*from  ww  w.j  ava  2s. co  m*/
 * Note: for compatibility with the original CONFOR significant figures are only applied to values after
 * the decimal place. (e.g. 123456.7 will be formatted as 123456, not 123460)
 * @param number the number to format.
 * @return the supplied number as a String.
 */
@Override
public String formatNumber(BigDecimal number) {

    int significantFigures = determinePrecision(number);
    MathContext context = new MathContext(significantFigures, RoundingMode.HALF_UP);

    BigDecimal result = number.round(context);
    result = result.stripTrailingZeros();
    return result.toPlainString();
}

From source file:edu.harvard.hul.ois.fits.EbuCoreNormalizedRatio.java

/**
 * Returns the number of decimal places in the number.
 *
 * @param bigDec//from w w  w  . j  a v a2  s .c  o m
 * @return
 */
private int getNumberOfDecimalPlaces(BigDecimal bigDec) {
    String strTrim = bigDec.stripTrailingZeros().toPlainString();
    int index = strTrim.indexOf(DECIMAL);
    return index < 0 ? 0 : strTrim.length() - index - 1;
}

From source file:co.nubetech.apache.hadoop.TextSplitter.java

/**
 * Return the string encoded in a BigDecimal. Repeatedly multiply the input
 * value by 65536; the integer portion after such a multiplication
 * represents a single character in base 65536. Convert that back into a
 * char and create a string out of these until we have no data left.
 *//*from  w w w .  ja v a2 s .  c om*/
String bigDecimalToString(BigDecimal bd) {
    BigDecimal cur = bd.stripTrailingZeros();
    StringBuilder sb = new StringBuilder();

    for (int numConverted = 0; numConverted < MAX_CHARS; numConverted++) {
        cur = cur.multiply(ONE_PLACE);
        int curCodePoint = cur.intValue();
        if (0 == curCodePoint) {
            break;
        }

        cur = cur.subtract(new BigDecimal(curCodePoint));
        sb.append(Character.toChars(curCodePoint));
    }

    return sb.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;/*from w w  w  . j  a v a 2 s. c  o  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:com.thinkbiganalytics.spark.validation.HCatDataType.java

private int getNumberOfDecimalPlaces(BigDecimal bigDecimal) {
    String string = bigDecimal.stripTrailingZeros().toPlainString();
    int index = string.indexOf(".");
    return index < 0 ? 0 : string.length() - index - 1;
}

From source file:com.stratio.cassandra.lucene.schema.mapping.BigDecimalMapper.java

/** {@inheritDoc} */
@Override//from   w  w w  . j  av a2 s .com
protected String doBase(String name, Object value) {

    // Parse big decimal
    BigDecimal bd;
    try {
        bd = new BigDecimal(value.toString());
    } catch (NumberFormatException e) {
        throw new IndexException("Field '%s' requires a base 10 decimal, but found '%s'", name, value);
    }

    // Split integer and decimal part
    bd = bd.stripTrailingZeros();
    String[] parts = bd.toPlainString().split("\\.");
    validateIntegerPart(name, value, parts);
    validateDecimalPart(name, value, parts);

    BigDecimal complemented = bd.add(complement);
    String bds[] = complemented.toString().split("\\.");
    String integerPart = StringUtils.leftPad(bds[0], integerDigits + 1, '0');
    String decimalPart = bds.length == 2 ? bds[1] : "0";

    return integerPart + "." + decimalPart;
}