Example usage for java.math RoundingMode HALF_UP

List of usage examples for java.math RoundingMode HALF_UP

Introduction

In this page you can find the example usage for java.math RoundingMode HALF_UP.

Prototype

RoundingMode HALF_UP

To view the source code for java.math RoundingMode 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:es.logongas.encuestas.modelo.resultados.EstadisticaDescriptiva.java

private BigDecimal getBigDecimal(double doubleData) {
    BigDecimal bigDecimalData = new BigDecimal(doubleData).setScale(numDecimals, RoundingMode.HALF_UP);

    return bigDecimalData;
}

From source file:cloudnet.sla.VmSlaOverallDowntime.java

public static double round(double value, int places) {
    if (places < 0) {
        throw new IllegalArgumentException();
    }/*from   w  ww  .ja v  a 2s .c o m*/

    BigDecimal bd = new BigDecimal(value);
    bd = bd.setScale(places, RoundingMode.HALF_UP);
    return bd.doubleValue();
}

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

/**
 * Converts BigDecimal latitude longitude to UTM 
 * /*from w  w  w . j  a v a  2s.c  om*/
 * @param latitude
 * @param longitude
 * @param datumName
 * @return UTM
 * @throws java.lang.Exception
 * 
 * 
 */
public static UTM convert(BigDecimal latitude, BigDecimal longitude, String datumName) throws Exception {

    Datum datum = Datum.valueOf(datumName);

    BigDecimal meridianRadius = new BigDecimal(datum.getMeridianRadius());
    BigDecimal eccentricity = new BigDecimal(datum.getEccentricity());

    BigDecimal latitudeRadians = latitude.abs().multiply(new BigDecimal(Math.PI)).divide(new BigDecimal(180.0),
            PRECISION, RoundingMode.HALF_UP);

    int zoneNumber = calcZoneNumber(longitude);

    BigDecimal zoneCentralMeridian = calcZoneCentralMeridian(zoneNumber);

    BigDecimal changeInLongitudeDegree = (longitude.subtract(zoneCentralMeridian)).abs().setScale(PRECISION,
            RoundingMode.HALF_UP);

    BigDecimal changeInLongitudeRadians = (changeInLongitudeDegree.multiply(new BigDecimal(Math.PI)))
            .divide(new BigDecimal(180), PRECISION, RoundingMode.HALF_UP);

    BigDecimal conformalLatitude = calcConformalLatitude(eccentricity, latitudeRadians).setScale(PRECISION,
            RoundingMode.HALF_UP);

    BigDecimal tauPrime = (new BigDecimal(Math.tan(conformalLatitude.doubleValue()))).setScale(PRECISION,
            RoundingMode.HALF_UP);

    BigDecimal xiPrimeNorth = calcXiPrimeNorth(changeInLongitudeRadians, tauPrime).setScale(PRECISION,
            RoundingMode.HALF_UP);

    BigDecimal etaPrimeEast = calcEtaPrimeEast(changeInLongitudeRadians, tauPrime).setScale(PRECISION,
            RoundingMode.HALF_UP);

    double[] alphaSeries = datum.getAlphaSeries();

    BigDecimal xiNorth = calcXiNorth(xiPrimeNorth, etaPrimeEast, alphaSeries).setScale(PRECISION,
            RoundingMode.HALF_UP);

    BigDecimal etaEast = calcEtaEast(xiPrimeNorth, etaPrimeEast, alphaSeries).setScale(PRECISION,
            RoundingMode.HALF_UP);

    BigDecimal easting = calcEasting(meridianRadius, etaEast, longitude, zoneCentralMeridian)
            .setScale(PRECISION, RoundingMode.HALF_UP);
    BigDecimal northing = calcNorthing(meridianRadius, xiNorth, latitude).setScale(PRECISION,
            RoundingMode.HALF_UP);

    char zoneLetter = calcZoneLetter(latitude);
    char hemisphere = calcHemisphere(latitude);

    if (easting.doubleValue() > UTM.MAX_EASTING)
        easting = new BigDecimal(UTM.MAX_EASTING);
    if (easting.doubleValue() < UTM.MIN_EASTING)
        easting = new BigDecimal(UTM.MIN_EASTING);

    if (northing.doubleValue() > UTM.MAX_NORTHING)
        northing = new BigDecimal(UTM.MAX_NORTHING);
    if (northing.doubleValue() < UTM.MIN_NORTHING)
        northing = new BigDecimal(UTM.MIN_NORTHING);

    UTM utm = new UTM(easting.setScale(SCALE, RoundingMode.HALF_UP),
            northing.setScale(SCALE, RoundingMode.HALF_UP), hemisphere, zoneNumber, zoneLetter);

    return utm;
}

From source file:org.projectforge.web.wicket.converter.BigDecimalPercentConverter.java

@Override
public BigDecimal convertToObject(String value, final Locale locale) {
    value = StringUtils.trimToEmpty(value);
    if (value.endsWith("%") == true) {
        value = value.substring(0, value.length() - 1).trim();
    }/*from w w  w  .ja va  2  s .co  m*/
    BigDecimal bd = super.convertToObject(value, locale);
    if (bd != null && decimalFormat == true) {
        bd = bd.divide(NumberHelper.HUNDRED, bd.scale() + 2, RoundingMode.HALF_UP);
    }
    return bd;
}

From source file:org.homiefund.test.dao.TransactionDAOTest.java

@Test
public void create() {
    Transaction tx = new Transaction();
    tx.setRevoked(false);//from   w ww .  j a  v  a 2  s . c  om
    tx.setDate(LocalDate.now());
    tx.setDescription(" create tessst ");
    tx.setOwner(userDAO.getById(1L));
    tx.setTransactionType(transactionTypeDAO.getById(4L));
    tx.setAmount(BigDecimal.valueOf(30.52));
    TransactionParticipant tp1 = new TransactionParticipant();
    tp1.setParticipant(userDAO.getById(1L));
    TransactionParticipant tp2 = new TransactionParticipant();
    tp2.setParticipant(userDAO.getById(2L));
    tx.setParticipants(new ArrayList<>(Arrays.asList(tp1, tp2)));

    Assert.assertNotNull(transactionDAO.create(tx));
    Assert.assertNotNull(tx.getId());

    BigDecimal half = tx.getAmount().divide(BigDecimal.valueOf(2L), new MathContext(2, RoundingMode.HALF_UP));
    tx.getParticipants().forEach(p -> Assert.assertEquals(half, p.getAmount()));
}

From source file:org.renjin.primitives.MathExt.java

@Deferrable
@Builtin//from   ww  w.j av  a  2s .c  o m
@DataParallel
public static double signif(double x, int digits) {
    return new BigDecimal(x).round(new MathContext(digits, RoundingMode.HALF_UP)).doubleValue();
}

From source file:com.webbfontaine.valuewebb.model.util.Utils.java

public static RoundingMode getRoundingMode() {
    String roundingMode = ApplicationProperties.getRoundingMode();
    if ("HALF_UP".equals(roundingMode) || roundingMode == null) {
        return RoundingMode.HALF_UP;
    }//from  w w w . j av  a 2s.co m
    if ("HALF_DOWN".equals(roundingMode)) {
        return RoundingMode.HALF_DOWN;
    }
    throw new IllegalArgumentException(String.format("Unsupported rounding mode: %s", roundingMode));
}

From source file:org.projectforge.business.fibu.datev.BuchungssatzImportRow.java

public void setBetrag(BigDecimal betrag) {
    this.betrag = betrag != null ? betrag.setScale(2, RoundingMode.HALF_UP) : null;
}

From source file:com.urielsarrazin.ppc.Command.java

private String formatResultForDisplay(List<PartialPressuresCalculResult> partialPressuresCalculResults) {

    final StringBuilder result = new StringBuilder();

    for (PartialPressuresCalculResult partialPressuresCalculResult : partialPressuresCalculResults) {

        result.append(partialPressuresCalculResult.getBreathingMix()).append("\t")
                .append(partialPressuresCalculResult.getDepth().getMeters());

        for (Map.Entry<Gas, Pressure> gasPartialPressure : partialPressuresCalculResult
                .getPartialPressuresPerGas().entrySet())
            result.append("\t").append(gasPartialPressure.getKey()).append("\t")
                    .append(new BigDecimal(gasPartialPressure.getValue().getBar())
                            .setScale(2, RoundingMode.HALF_UP).doubleValue());

        result.append(OsUtils.LINE_SEPARATOR);
    }//from w  ww .j a  v a 2s . c o  m

    return result.toString();
}

From source file:com.ibk.ltw.domain.Product.java

/**
 *
 * @return Price with VAT//from  w ww.  jav a2s. com
 */
public int getGrossPriceInCents() {
    BigDecimal grossPriceInCents = new BigDecimal(netPriceInCents)
            .multiply(new BigDecimal(1000 + vatService.getVatInPerThousand())).movePointLeft(3);
    return grossPriceInCents.round(new MathContext(0, RoundingMode.HALF_UP)).intValue();
}