Example usage for java.math BigDecimal ROUND_HALF_UP

List of usage examples for java.math BigDecimal ROUND_HALF_UP

Introduction

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

Prototype

int ROUND_HALF_UP

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

Click Source Link

Document

Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up.

Usage

From source file:com.cerema.cloud2.utils.DisplayUtils.java

/**
 * Converts the file size in bytes to human readable output.
 * <ul>//from   w w  w  .jav  a2s  .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) {
    double result = bytes;
    int attachedSuff = 0;
    while (result > 1024 && attachedSuff < sizeSuffixes.length) {
        result /= 1024.;
        attachedSuff++;
    }

    return new BigDecimal(result).setScale(sizeScales[attachedSuff], BigDecimal.ROUND_HALF_UP) + " "
            + sizeSuffixes[attachedSuff];
}

From source file:org.estatio.app.budget.Status.java

public BudgetKeyItemImportExportLineItem(final BudgetKeyItemImportExportLineItem item) {
    this.budgetKeyItem = item.budgetKeyItem;
    this.unitReference = item.unitReference;
    this.status = item.status;
    this.sourceValue = item.sourceValue.setScale(2, BigDecimal.ROUND_HALF_UP);
    this.keyValue = item.keyValue.setScale(3, BigDecimal.ROUND_HALF_UP);
    this.augmentedKeyValue = item.augmentedKeyValue.setScale(6, BigDecimal.ROUND_HALF_UP);
    this.budgetKeyTableName = item.budgetKeyTableName;
}

From source file:net.cloudkit.enterprises.infrastructure.freemarker.method.CurrencyMethod.java

@SuppressWarnings("rawtypes")
public Object exec(List arguments) {
    if ((arguments != null) && (!arguments.isEmpty()) && (arguments.get(0) != null)
            && (StringUtils.isNotEmpty(arguments.get(0).toString()))) {
        boolean bool1 = false;
        boolean bool2 = false;
        if (arguments.size() == 2) {
            if (arguments.get(1) != null) {
                bool1 = Boolean.valueOf(arguments.get(1).toString()).booleanValue();
            }/*from   ww  w  .ja  v  a 2s.  com*/
        } else if (arguments.size() > 2) {
            if (arguments.get(1) != null) {
                bool1 = Boolean.valueOf(arguments.get(1).toString()).booleanValue();
            }
            if (arguments.get(2) != null) {
                bool2 = Boolean.valueOf(arguments.get(2).toString()).booleanValue();
            }
        }

        // BigDecimal.setScale()??
        // setScale(1)?????
        // setScale(1, BigDecimal.ROUND_DOWN)??2.35??2.3
        // setScale(1, BigDecimal.ROUND_UP)??2.35??2.4
        // setScale(1, BigDecimal.ROUND_HALF_UP)?2.35??2.4
        // setScale(1, BigDecimal.ROUND_HALF_DOWN)?2.35??2.35??
        BigDecimal localBigDecimal = new BigDecimal(arguments.get(0).toString());
        // newScale:PriceScale 2, roundingMode:PriceRoundType BigDecimal.ROUND_HALF_UP
        String str = localBigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).toString();
        // currencySign
        if (bool1) {
            str = "" + str;
        }
        // currencyUnit
        if (bool2) {
            str = str + "";
        }
        return new SimpleScalar(str);
    }
    return null;
}

From source file:com.logsniffer.web.tags.JstlFunctionsLibrary.java

/**
 * Rounds given floating point value taking the precision into account.
 * //from w  w  w.j  av a2 s.  co m
 * @param value
 * @param precision
 * @return rounded value
 */
public static double round(final double value, final int precision) {
    return new BigDecimal(value).setScale(precision, BigDecimal.ROUND_HALF_UP).doubleValue();
}

From source file:com.sinosoft.one.showcase.rule.test.RuleServiceTest.java

@Test
public void testQuickPriceService() {
    CarInfoInputBOM qpib = new CarInfoInputBOM();
    qpib.setAreaCode("370000");
    qpib.setVehicleAge(1);/*w  w  w .j a v  a2s  . c  o m*/
    qpib.setReplacementValue(20000.00);
    QuickPriceInputGlobal global = new QuickPriceInputGlobal();
    double totalPremium = 0.00;
    try {
        System.out.println("=========begin QuickPriceRule");
        quickPriceRuleService.executeRuleWithGlobal("quickPriceRuleFlow", global, qpib);
        // quickPriceRuleService.executeRule( qpib);
        System.out.println("=========end QuickPriceRule");

        Iterator iter = global.getKinds().entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            String kindcode = (String) entry.getKey();
            KindBOM kind = (KindBOM) entry.getValue();
            System.out.println(kindcode + " name=" + kind.getKindName());
            System.out.print(kindcode + "amount=" + kind.getAmount());
            System.out.print(kindcode + "premium=" + kind.getPremium());
            double premium = new BigDecimal(kind.getPremium() * global.getDiscount())
                    .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            System.out.println(kindcode + "discount premium=" + premium);
            totalPremium += premium;
        }
        System.out.println(" totalPremium  " + totalPremium);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Assert.assertTrue(totalPremium > 0.0);
}

From source file:org.cirdles.ambapo.UTMToLatLong.java

/**
 * Converts a UTM into a Coordinate of lat,long with a specific datum.
 * @param utm/*from   ww w .  java  2 s.  co  m*/
 * @param datum
 * @return Coordinate
 * @throws Exception 
 */
public static Coordinate convert(UTM utm, String datum) throws Exception {

    Datum datumInformation = Datum.valueOf(datum);

    double[] betaSeries = datumInformation.getBetaSeries();

    char hemisphere = utm.getHemisphere();

    double zoneCentralMeridian = utm.getZoneNumber() * 6 - 183;

    BigDecimal meridianRadius = new BigDecimal(datumInformation.getMeridianRadius());

    BigDecimal northing = utm.getNorthing();

    BigDecimal easting = utm.getEasting();

    BigDecimal xiNorth = calcXiNorth(hemisphere, meridianRadius, northing);

    BigDecimal etaEast = calcEtaEast(easting, meridianRadius);

    BigDecimal xiPrime = calcXiPrime(xiNorth, etaEast, betaSeries);

    BigDecimal etaPrime = calcEtaPrime(xiNorth, etaEast, betaSeries);

    BigDecimal tauPrime = calcTauPrime(xiPrime, etaPrime);

    BigDecimal eccentricity = new BigDecimal(datumInformation.getEccentricity());

    BigDecimal sigma = calcSigma(eccentricity, tauPrime);

    BigDecimal longitude = calcLongitude(zoneCentralMeridian, etaPrime, xiPrime);

    if (longitude.doubleValue() > Coordinate.MAX_LONGITUDE)
        longitude = new BigDecimal(Coordinate.MAX_LONGITUDE);

    if (longitude.doubleValue() < Coordinate.MIN_LONGITUDE)
        longitude = new BigDecimal(Coordinate.MIN_LONGITUDE);

    BigDecimal latitude = calcLatitude(tauPrime, sigma, eccentricity, hemisphere);

    if (latitude.doubleValue() > Coordinate.MAX_LATITUDE)
        latitude = new BigDecimal(Coordinate.MAX_LATITUDE);
    if (latitude.doubleValue() < Coordinate.MIN_LATITUDE)
        latitude = new BigDecimal(Coordinate.MIN_LATITUDE);

    Coordinate latAndLong = new Coordinate(latitude.setScale(SCALE, BigDecimal.ROUND_HALF_UP),
            longitude.setScale(SCALE, BigDecimal.ROUND_HALF_UP), datum);

    return latAndLong;

}

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

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

From source file:org.finra.herd.core.AbstractCoreTest.java

/**
 * Returns a random big decimal./*from  www. j ava2 s. c  o m*/
 */
public static BigDecimal getRandomBigDecimal() {
    return new BigDecimal(Math.random() * Integer.MAX_VALUE).setScale(2, BigDecimal.ROUND_HALF_UP);
}

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

/**
 * Converts the file size in bytes to human readable output.
 * <ul>/*w w w .ja v  a  2 s . com*/
 *     <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:com.fengduo.bee.commons.util.NumberParser.java

public static double div(double a, double b, int scale) {
    if (scale < 0) {
        throw new IllegalArgumentException("The scale must be a positive integer or zero");
    }//from   w ww. j av  a 2s . c  o m
    BigDecimal b1 = new BigDecimal(Double.toString(a));
    BigDecimal b2 = new BigDecimal(Double.toString(b));
    return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
}