Example usage for java.math BigDecimal setScale

List of usage examples for java.math BigDecimal setScale

Introduction

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

Prototype

@Deprecated(since = "9")
public BigDecimal setScale(int newScale, int roundingMode) 

Source Link

Document

Returns a BigDecimal whose scale is the specified value, and whose unscaled value is determined by multiplying or dividing this BigDecimal 's unscaled value by the appropriate power of ten to maintain its overall value.

Usage

From source file:com.spaceprogram.simplejpa.util.AmazonSimpleDBUtil.java

public static String encodeRealNumberRange(BigDecimal number, int maxDigitsLeft, int maxDigitsRight,
        BigDecimal offsetValue) {
    BigDecimal shiftMultiplier = new BigDecimal(Math.pow(10, maxDigitsRight));
    //        System.out.println("shiftMultiplier=" + shiftMultiplier);
    BigDecimal shiftedNumber = number.multiply(shiftMultiplier);
    //        System.out.println("shiftedNumber=" + shiftedNumber);
    shiftedNumber = shiftedNumber.setScale(0, BigDecimal.ROUND_HALF_UP);
    //        System.out.println("shiftedNumber rounded=" + shiftedNumber);
    BigDecimal shiftedOffset = offsetValue.multiply(shiftMultiplier);
    //        System.out.println("shiftedOffset=" + shiftedOffset);
    BigDecimal offsetNumber = shiftedNumber.add(shiftedOffset);
    //        System.out.println("offsetNumber=" + offsetNumber);
    String longString = offsetNumber.toString();
    //        System.out.println("shifted string=" + longString);
    int numBeforeDecimal = longString.length();
    int numZeroes = maxDigitsLeft + maxDigitsRight - numBeforeDecimal;
    StringBuffer strBuffer = new StringBuffer(numZeroes + longString.length());
    for (int i = 0; i < numZeroes; i++) {
        strBuffer.insert(i, '0');
    }//from  ww  w .  jav  a  2s .  co  m
    strBuffer.append(longString);
    return strBuffer.toString();
}

From source file:org.bankinterface.util.Utils.java

/**
 * ??,??,,??./*from   www.  j  a  v a  2  s.  c o m*/
 * 
 * @param amount
 * @return
 */
public static String amountToYuan(BigDecimal amount) {
    if (amount == null) {
        throw new IllegalArgumentException();
    }
    return amount.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString();
}

From source file:morphy.utils.MorphyStringUtils.java

/**
 * Returns the value specified in bytes into megs. Currently only shows 2
 * digits after the decimal and rounds half up.
 *///from   w ww .  j a v  a2  s .c  om
public static String getMegs(long bytes) {
    BigDecimal bigDecimal = new BigDecimal(bytes / 1048576.0);
    bigDecimal = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP);
    return bigDecimal.toString() + "Megs";
}

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   ww  w. j a  va 2  s  .  c  om*/
    }
    return bigDecimal;
}

From source file:com.epam.parso.impl.CSVDataWriterImpl.java

/**
 * The function to convert a double value into a string. If the text presentation of the double is longer
 * than {@link CSVDataWriterImpl#ROUNDING_LENGTH}, the rounded off value of the double includes
 * the {@link CSVDataWriterImpl#ACCURACY} number of digits from the first non-zero value.
 *
 * @param value the input numeric value to convert.
 * @return the string with the text presentation of the input numeric value.
 *//*w w  w  .j a  va 2  s . com*/
protected static String convertDoubleElementToString(Double value) {
    String valueToPrint = String.valueOf(value);
    if (valueToPrint.length() > ROUNDING_LENGTH) {
        int lengthBeforeDot = (int) Math.ceil(Math.log10(Math.abs(value)));
        BigDecimal bigDecimal = new BigDecimal(value);
        bigDecimal = bigDecimal.setScale(ACCURACY - lengthBeforeDot, BigDecimal.ROUND_HALF_UP);
        valueToPrint = String.valueOf(bigDecimal.doubleValue());
    }
    valueToPrint = trimZerosFromEnd(valueToPrint);
    return valueToPrint;
}

From source file:com.base.dao.sql.ReflectionUtils.java

private static double processDecimal(Object value) {
    String objs = String.valueOf(value);
    double dbvalue = Double.valueOf(objs);
    BigDecimal bg = new BigDecimal(dbvalue);
    double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    return f1;//  www . ja v a2 s.  c  o m
}

From source file:com.fengduo.bee.commons.util.StringFormatter.java

public static String float2format(Double d) {
    if (d == null) {
        return ZERO_STR;
    }//from ww  w. jav  a 2 s .  co m

    BigDecimal b = new BigDecimal(d);
    float fb = b.setScale(SCALE_TWO, BigDecimal.ROUND_DOWN).floatValue();
    DecimalFormat format = new DecimalFormat(FORMAT);
    return format.format(fb).toString();
}

From source file:org.kalypso.model.wspm.tuhh.ui.utils.GuessStationPatternReplacer.java

public static BigDecimal findStation(final String searchString, final GuessStationContext[] searchContexts,
        final Pattern[] searchPatterns) {
    for (int i = 0; i < searchPatterns.length; i++) {
        final Matcher matcher = searchPatterns[i].matcher(searchString);
        if (matcher.matches()) {
            final String stationString = matcher.group(1);

            final BigDecimal station = searchContexts[i].parseStation(stationString);
            if (station == null)
                return null;

            // Set scale to 1, as it is in the database, else we get problems to compare
            // with existing cross sections
            return station.setScale(1, BigDecimal.ROUND_HALF_UP);
        }/*from   ww  w .  j  a  v  a 2 s.co m*/
    }

    return null;
}

From source file:hudson.plugins.robot.model.RobotResult.java

private static double roundToDecimals(double value, int decimals) {
    BigDecimal bd = new BigDecimal(Double.toString(value));
    bd = bd.setScale(decimals, BigDecimal.ROUND_DOWN);
    return bd.doubleValue();
}

From source file:com.salesmanager.core.util.CurrencyUtil.java

/**
 * Get the measure according to the appropriate measure base. If the measure
 * configured in store is LB and it needs KG then the appropriate
 * calculation is done/* ww w.j a  v a2s . c  o m*/
 * 
 * @param weight
 * @param store
 * @param base
 * @return
 */
public static double getWeight(double weight, MerchantStore store, String base) {

    double weightConstant = 2.2;
    if (base.equals(Constants.LB_WEIGHT_UNIT)) {
        if (store.getWeightunitcode().equals(Constants.LB_WEIGHT_UNIT)) {
            return new BigDecimal(String.valueOf(weight)).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        } else {// pound = kilogram
            double answer = weight * weightConstant;
            BigDecimal w = new BigDecimal(answer);
            return w.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        }
    } else {// need KG
        if (store.getWeightunitcode().equals(Constants.KG_WEIGHT_UNIT)) {
            return new BigDecimal(String.valueOf(weight)).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        } else {

            double answer = weight / weightConstant;
            BigDecimal w = new BigDecimal(answer);
            return w.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

        }
    }
}