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:org.eclipse.smarthome.binding.astro.internal.util.PropertyUtils.java

/**
 * Returns the state of the channel.//from  w  w w . ja va2 s  . com
 */
public static State getState(ChannelUID channelUID, AstroChannelConfig config, Object instance)
        throws Exception {
    Object value = getPropertyValue(channelUID, instance);
    if (value == null) {
        return UnDefType.UNDEF;
    } else if (value instanceof State) {
        return (State) value;
    } else if (value instanceof Calendar) {
        Calendar cal = (Calendar) value;
        GregorianCalendar gregorianCal = (GregorianCalendar) DateTimeUtils.applyConfig(cal, config);
        cal.setTimeZone(TimeZone.getTimeZone(timeZoneProvider.getTimeZone()));
        ZonedDateTime zoned = gregorianCal.toZonedDateTime().withFixedOffsetZone();
        return new DateTimeType(zoned);
    } else if (value instanceof Number) {
        BigDecimal decimalValue = new BigDecimal(value.toString()).setScale(2, RoundingMode.HALF_UP);
        return new DecimalType(decimalValue);
    } else if (value instanceof String || value instanceof Enum) {
        return new StringType(value.toString());
    } else {
        throw new IllegalStateException("Unsupported value type " + value.getClass().getSimpleName());
    }
}

From source file:com.sunchenbin.store.feilong.core.text.NumberFormatUtil.java

/**
 *  {@link Number}  numberPattern?./*from w w w  . j ava  2  s .c  om*/
 * 
 * <p>
 *  {@link java.math.RoundingMode#HALF_UP}
 * </p>
 *
 * @param value
 *            the value
 * @param numberPattern
 *            the pattern {@link NumberPattern}
 * @return  null
 * @see NumberPattern
 * @see DecimalFormat
 * @see RoundingMode#HALF_UP
 */
public static String format(Number value, String numberPattern) {
    // ?, DecimalFormat RoundingMode.HALF_EVEN
    RoundingMode roundingMode = RoundingMode.HALF_UP;
    return format(value, numberPattern, roundingMode);
}

From source file:r.base.MathExt.java

@Primitive
public static double signif(@Recycle double x, @Recycle int digits) {
    return new BigDecimal(x).round(new MathContext(digits, RoundingMode.HALF_UP)).doubleValue();
}

From source file:org.openvpms.archetype.rules.math.MathRules.java

/**
 * Performs a division, rounding the result to the specified no. of places.
 *
 * @param dividend the value to divide/*ww w  . j a  v  a2s  .  c o m*/
 * @param divisor  the divisor
 * @param scale    the no. of decimal places
 * @return the divided value
 */
public static BigDecimal divide(BigDecimal dividend, BigDecimal divisor, int scale) {
    return dividend.divide(divisor, scale, RoundingMode.HALF_UP);
}

From source file:services.kpi.PortfolioEntryAllocationProgressKpi.java

@Override
public BigDecimal computeMain(IPreferenceManagerPlugin preferenceManagerPlugin, IScriptService scriptService,
        Kpi kpi, Long objectId) {

    BigDecimal timesheetedDays = computeAdditional1(preferenceManagerPlugin, scriptService, kpi, objectId)
            .setScale(2, RoundingMode.HALF_UP);

    BigDecimal forecastDays = computeAdditional2(preferenceManagerPlugin, scriptService, kpi, objectId);

    if (forecastDays.compareTo(BigDecimal.ZERO) > 0) {
        return timesheetedDays.divide(forecastDays, 2, RoundingMode.HALF_UP).multiply(new BigDecimal(100));
    }/*from  w  w  w  .  j av a2s.co  m*/

    return null;

}

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

public static UTM convert(BigDecimal latitude, BigDecimal longitude, String datumName) {

    DatumEnum datumEnum = DatumEnum.valueOf(datumName);
    BigDecimal flattening3D = new BigDecimal(datumEnum.getFlattening3D());
    BigDecimal meridianRadius = new BigDecimal(datumEnum.getMeridianRadius());
    BigDecimal eccentricity = new BigDecimal(datumEnum.getEccentricity());

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

    //System.out.println("Latitude Radians: " + latitudeRadians);

    int zoneNumber = calcZoneNumber(longitude);

    BigDecimal zoneCentralMeridian = calcZoneCentralMeridian(zoneNumber);

    BigDecimal changeInLongitudeDegree = (longitude.subtract(zoneCentralMeridian)).abs().setScale(precision,
            RoundingMode.HALF_UP);
    //System.out.println("Change in Long Degree: " + changeInLongitudeDegree);

    BigDecimal changeInLongitudeRadians = (changeInLongitudeDegree.multiply(new BigDecimal(Math.PI)))
            .divide(new BigDecimal(180), precision, RoundingMode.HALF_UP);
    //System.out.println("Change In Longitude Radians: " + changeInLongitudeRadians);

    BigDecimal conformalLatitude = calcConformalLatitude(eccentricity, latitudeRadians).setScale(precision,
            RoundingMode.HALF_UP);
    //System.out.println("Conformal Latitude: " + conformalLatitude);

    BigDecimal tauPrime = (new BigDecimal(Math.tan(conformalLatitude.doubleValue()))).setScale(precision,
            RoundingMode.HALF_UP);
    //System.out.println("Tau Prime: " + tauPrime);

    BigDecimal xiPrimeNorth = calcXiPrimeNorth(changeInLongitudeRadians, tauPrime).setScale(precision,
            RoundingMode.HALF_UP);
    //System.out.println("xi Prime North: " + xiPrimeNorth);

    BigDecimal etaPrimeEast = calcEtaPrimeEast(changeInLongitudeRadians, tauPrime).setScale(precision,
            RoundingMode.HALF_UP);
    //System.out.println("Eta Prime East: " + etaPrimeEast);

    BigDecimal[] alphaSeries = { KrugerSeries.alpha1(flattening3D).setScale(precision, RoundingMode.HALF_UP),
            KrugerSeries.alpha2(flattening3D.setScale(precision, RoundingMode.HALF_UP)),
            KrugerSeries.alpha3(flattening3D).setScale(precision, RoundingMode.HALF_UP),
            KrugerSeries.alpha4(flattening3D).setScale(precision, RoundingMode.HALF_UP),
            KrugerSeries.alpha5(flattening3D).setScale(precision, RoundingMode.HALF_UP),
            KrugerSeries.alpha6(flattening3D).setScale(precision, RoundingMode.HALF_UP),
            KrugerSeries.alpha7(flattening3D).setScale(precision, RoundingMode.HALF_UP) };

    BigDecimal xiNorth = calcXiNorth(xiPrimeNorth, etaPrimeEast, alphaSeries).setScale(precision,
            RoundingMode.HALF_UP);
    //System.out.println("xi North: " + xiNorth);

    BigDecimal etaEast = calcEtaEast(xiPrimeNorth, etaPrimeEast, alphaSeries).setScale(precision,
            RoundingMode.HALF_UP);
    //System.out.println("Eta East: " + etaEast);

    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);

    return new UTM(easting, northing, hemisphere, zoneNumber, zoneLetter);

}

From source file:services.kpi.PortfolioEntryActualsKpi.java

@Override
public BigDecimal computeMain(IPreferenceManagerPlugin preferenceManagerPlugin, IScriptService scriptService,
        Kpi kpi, Long objectId) {
    Double entryEngagedCapex = PortfolioEntryDao.getPEAsEngagedAmountByOpex(preferenceManagerPlugin, objectId,
            false);//from www.  j  a  v  a  2 s.c  o m
    Double entryEngagedOpex = PortfolioEntryDao.getPEAsEngagedAmountByOpex(preferenceManagerPlugin, objectId,
            true);
    BigDecimal engaged = (new BigDecimal(entryEngagedCapex + entryEngagedOpex)).setScale(2,
            RoundingMode.HALF_UP);
    ;
    return engaged;
}

From source file:org.renjin.MathExt.java

public static double signif(double x, int digits) {
    return new BigDecimal(x).round(new MathContext(digits, RoundingMode.HALF_UP)).doubleValue();
}

From source file:com.ar.dev.tierra.api.dao.impl.FiscalDAOImpl.java

@Override
public void ticket(List<DetalleFactura> detalles) {
    try (PrintWriter ticket = new PrintWriter("command/ticket.200")) {
        DecimalFormat decimalFormat = new DecimalFormat();
        decimalFormat.setMaximumFractionDigits(1);
        ticket.println("@" + (char) 28 + "T" + (char) 28 + "T");
        BigDecimal descuento = new BigDecimal(BigInteger.ZERO);
        for (DetalleFactura detalle : detalles) {
            if (detalle.getDescuentoDetalle() != null) {
                descuento = descuento.add(detalle.getDescuentoDetalle());
            }//ww w  .ja  v  a 2s  . c o  m
            String price = null;
            BigDecimal sinIVA = detalle.getProducto().getPrecioVenta().subtract(detalle.getProducto()
                    .getPrecioVenta().multiply(new BigDecimal(17.35)).divide(new BigDecimal(100)));
            price = sinIVA.setScale(4, RoundingMode.HALF_UP).toString();
            ticket.println("B" + (char) 28 /*Abrimos linea*/
                    + detalle.getProducto().getDescripcion() + (char) 28 /*Nombre producto*/
                    + detalle.getCantidadDetalle() + ".0" + (char) 28 /*Cantidad*/
                    + price.replace(",", ".") + (char) 28 /*Precio unitario*/
                    + "21.0" + (char) 28 /*Impuestos IVA*/
                    + "M" + (char) 28 /*Suma monto*/
                    + "0.0" + (char) 28 /*Impuestos internos*/
                    + "0" + (char) 28 /*Parametro display*/
                    + "b");
            /*Cierra de linea*/
        }
        if (!descuento.equals(new BigDecimal(BigInteger.ZERO))) {
            ticket.println("T" + (char) 28 /*Abrimos linea descuento*/
                    + "Descuento: " + (char) 28 /*Texto a mostrar*/
                    + descuento + (char) 28 /*Monto descuento*/
                    + "m" + (char) 28 /*m: descuento, M: aumento*/
                    + "0" + (char) 28 /*parametro display*/
                    + "T");
            /*cierre linea descuento*/
        }
        ticket.println("E");
    } catch (FileNotFoundException ex) {
        Logger.getLogger(FiscalDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:es.logongas.encuestas.modelo.resultados.InferenciaEstadistica.java

public InferenciaEstadistica(EstadisticaDescriptiva estadisticaDescriptiva, BigDecimal nivelConfianza,
        int numDecimals) {
    this.numDecimals = numDecimals;
    if (nivelConfianza.compareTo(BigDecimal.ZERO) <= 0) {
        throw new IllegalArgumentException("El nivelConfianza debe ser mayor que 0");
    }/*from   w  ww.  j  av  a2 s .  c  o  m*/
    if (nivelConfianza.compareTo(BigDecimal.ONE) >= 0) {
        throw new IllegalArgumentException("El nivelConfianza debe ser menor que 1");
    }

    TDistribution tDistribution = new TDistribution(estadisticaDescriptiva.getNumMuestras() - 1);
    double t = tDistribution.inverseCumulativeProbability(nivelConfianza.doubleValue());
    BigDecimal delta = new BigDecimal(t * (estadisticaDescriptiva.getDesviacionEstandar().doubleValue()
            / Math.sqrt(estadisticaDescriptiva.getNumMuestras())));

    BigDecimal min = estadisticaDescriptiva.getMedia().subtract(delta).setScale(this.numDecimals,
            RoundingMode.HALF_UP);
    BigDecimal max = estadisticaDescriptiva.getMedia().add(delta).setScale(this.numDecimals,
            RoundingMode.HALF_UP);
    intervaloConfianzaMedia = new IntervaloConfianza(min, max, nivelConfianza);

}