Example usage for java.math RoundingMode FLOOR

List of usage examples for java.math RoundingMode FLOOR

Introduction

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

Prototype

RoundingMode FLOOR

To view the source code for java.math RoundingMode FLOOR.

Click Source Link

Document

Rounding mode to round towards negative infinity.

Usage

From source file:NumberFormatRounding.java

public static void main(String[] args) {
    NumberFormat nf = NumberFormat.getNumberInstance();
    nf.setMaximumFractionDigits(2);//  w  ww  .  j  a va2s  .  c om
    nf.setRoundingMode(RoundingMode.FLOOR);
    System.out.println("Default rounding mode: " + nf.getRoundingMode());
    System.out.println("123.454 rounds to " + nf.format(123.454));
    System.out.println("123.455 rounds to " + nf.format(123.455));
    System.out.println("123.456 rounds to " + nf.format(123.456));
    System.out.println();

}

From source file:Main.java

public static void main(String[] args) {

    BigDecimal bg1 = new BigDecimal("123.12678");

    // set scale of bg1 to 2 in bg2 using floor as rounding mode
    BigDecimal bg2 = bg1.setScale(2, RoundingMode.FLOOR);

    String str = bg1 + " after changing the scale to 2 and rounding is " + bg2;

    // print bg2 value
    System.out.println(str);//from   w  w  w .ja va 2s . com
}

From source file:NumberFormatRounding.java

public static void main(String[] args) {
    NumberFormat nf = NumberFormat.getNumberInstance();
    nf.setMaximumFractionDigits(2);//ww w.  ja  v a2  s  .co  m
    nf.setRoundingMode(RoundingMode.HALF_DOWN);
    System.out.println("Default rounding mode: " + nf.getRoundingMode());
    System.out.println("123.454 rounds to " + nf.format(123.454));
    System.out.println("123.455 rounds to " + nf.format(123.455));
    System.out.println("123.456 rounds to " + nf.format(123.456));
    System.out.println();

    nf.setRoundingMode(RoundingMode.FLOOR);
    System.out.println("Default rounding mode: " + nf.getRoundingMode());
    System.out.println("123.454 rounds to " + nf.format(123.454));
    System.out.println("123.455 rounds to " + nf.format(123.455));
    System.out.println("123.456 rounds to " + nf.format(123.456));
    System.out.println();

    nf.setMaximumFractionDigits(2);
    nf.setRoundingMode(RoundingMode.CEILING);
    System.out.println("Default rounding mode: " + nf.getRoundingMode());
    System.out.println("123.454 rounds to " + nf.format(123.454));
    System.out.println("123.455 rounds to " + nf.format(123.455));
    System.out.println("123.456 rounds to " + nf.format(123.456));
    System.out.println();

}

From source file:Main.java

/**
 * Rounds a double down via passed in amount and places
 * @param value//from  w w  w. j  a va 2  s. co m
 * @param places
 * @return
 */
public static double roundDown(double value, int places) {
    if (places < 0)
        throw new IllegalArgumentException();

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

From source file:Main.java

public static String kgToLBoz(float paramFloat) {
    float f1 = (32768.0F + 23117.0F * paramFloat) / 65536.0F;
    int i = (int) (f1 * 0.1F) / 16;
    float f2 = f1 * 0.1F % 16.0F;
    DecimalFormat localDecimalFormat = new DecimalFormat();
    localDecimalFormat.setMaximumFractionDigits(1);
    localDecimalFormat.setGroupingSize(0);
    localDecimalFormat.setRoundingMode(RoundingMode.FLOOR);
    return i + ":" + localDecimalFormat.format(f2);
}

From source file:Main.java

public static String kgToLB_ForBodyScale(float paramFloat) {
    int i = (int) Math.floor(1.0F + (32768.0F + 144479.0F * (paramFloat * 10.0F)) / 65536.0F);
    if (i % 2 != 0) {
        i--;//from w  w w.j  a va2  s.c om
    }
    DecimalFormat localDecimalFormat = new DecimalFormat();
    localDecimalFormat.setMaximumFractionDigits(1);
    localDecimalFormat.setGroupingSize(0);
    localDecimalFormat.setRoundingMode(RoundingMode.FLOOR);
    return localDecimalFormat.format(0.1D * i);
}

From source file:Main.java

/**
 * NOTE: Arithmetic operations in primitive types may lead to arithmetic overflow. To retain
 * precision, BigDecimal objects are used.
 *
 * @param rgb//from ww w  .ja  va 2s .  c  om
 * @return
 */
public static int[] convertRGB_8_8_8_To_HSB_32_32_32(int[] rgb) {
    int[] hsb = new int[3];
    int maxChroma = Math.max(Math.max(rgb[0], rgb[1]), rgb[2]);
    int minChroma = Math.min(Math.min(rgb[0], rgb[1]), rgb[2]);
    int diff = maxChroma - minChroma;

    // Hue
    BigDecimal hue;
    if (diff == 0) {
        hue = BigDecimal.ZERO;
    } else if (maxChroma == rgb[0]) {
        float tmp = (rgb[1] - rgb[2]) / (float) diff;
        if (tmp < 0) {
            tmp += 6 * Math.ceil(-tmp / 6.0);
        } else {
            tmp -= 6 * Math.floor(tmp / 6.0);
        }
        hue = BigDecimal.valueOf(tmp);
    } else if (maxChroma == rgb[1]) {
        hue = BigDecimal.valueOf((rgb[2] - rgb[0]) / (float) diff + 2);
    } else {
        hue = BigDecimal.valueOf((rgb[0] - rgb[1]) / (float) diff + 4);
    }
    // [0, 360] -> [0, 0xffffffff]
    hue = hue.multiply(BigDecimal.valueOf(0xffffffffL));
    hue = hue.divide(BigDecimal.valueOf(6), RoundingMode.FLOOR);
    hsb[0] = ByteBuffer.allocate(8).putLong(hue.longValue()).getInt(4);

    // Saturation
    if (maxChroma == 0) {
        hsb[1] = 0;
    } else {
        // [0, 1] -> [0, 0xffffffff]
        BigDecimal sat = BigDecimal.valueOf(diff);
        sat = sat.multiply(BigDecimal.valueOf(0xffffffffL));
        sat = sat.divide(BigDecimal.valueOf(maxChroma), RoundingMode.FLOOR);
        hsb[1] = ByteBuffer.allocate(8).putLong(sat.longValue()).getInt(4);
    }

    // Brightness
    // [0, 255] -> [0, 0xffffffff]
    BigDecimal brightness = BigDecimal.valueOf(maxChroma);
    brightness = brightness.multiply(BigDecimal.valueOf(0xffffffffL));
    brightness = brightness.divide(BigDecimal.valueOf(0xffL), RoundingMode.FLOOR);
    hsb[2] = ByteBuffer.allocate(8).putLong(brightness.longValue()).getInt(4);

    return hsb;
}

From source file:com.github.rinde.rinsim.scenario.ScenarioTestUtil.java

public static Scenario create(long seed) {
    final int endTime = 3 * 60 * 60 * 1000;
    Scenario.Builder b = Scenario.builder()
            .addModel(PlaneRoadModel.supplier(new Point(0, 0), new Point(10, 10), SI.KILOMETER,
                    Measure.valueOf(50d, NonSI.KILOMETERS_PER_HOUR)))
            .addModel(DefaultPDPModel.supplier(TimeWindowPolicies.LIBERAL)).addEvents(Collections.nCopies(10,
                    new AddVehicleEvent(-1, VehicleDTO.builder().startPosition(new Point(5, 5)).build())));

    RandomGenerator rng = new MersenneTwister(seed);
    for (int i = 0; i < 20; i++) {
        long announceTime = rng.nextInt(DoubleMath.roundToInt(endTime * .8, RoundingMode.FLOOR));
        b.addEvent(new AddParcelEvent(ParcelDTO
                .builder(new Point(rng.nextDouble() * 10, rng.nextDouble() * 10),
                        new Point(rng.nextDouble() * 10, rng.nextDouble() * 10))
                .orderAnnounceTime(announceTime).pickupTimeWindow(new TimeWindow(announceTime, endTime))
                .deliveryTimeWindow(new TimeWindow(announceTime, endTime)).neededCapacity(0).build()));
    }/* ww w  .ja v a  2 s  . co m*/

    b.addEvent(new TimedEvent(PDPScenarioEvent.TIME_OUT, endTime)).scenarioLength(endTime)
            .stopCondition(new EndTimeStopCondition(endTime));

    b.addEventType(PDPScenarioEvent.ADD_DEPOT);

    return b.build();
}

From source file:carrental.beans.billing.BillingBean.java

/**
 * @param servicePrice The standardized servicePrice is used as the mean for the randomized cost-calculation
 *///from ww  w  .  ja v a  2  s.co  m
private static BigDecimal simulateRepairCosts(double servicePrice) {
    Random randomGenerator = new Random();
    Double r = randomGenerator.nextGaussian() * 90 + servicePrice; //z*SD + mu
    return new BigDecimal(r).setScale(2, RoundingMode.FLOOR);
}

From source file:com.github.rinde.rinsim.pdptw.common.ScenarioTestUtil.java

/**
 * Creates a random scenario.//from   ww w.  j  ava 2  s  . c  om
 * @param seed The seed to use.
 * @param models Additional models (optional).
 * @return A new random scenario.
 */
public static Scenario createRandomScenario(long seed, ModelBuilder<?, ?>... models) {
    final int endTime = 3 * 60 * 60 * 1000;
    final Scenario.Builder b = Scenario.builder().addModel(RoadModelBuilders.plane())
            .addModel(DefaultPDPModel.builder()).addModels(asList(models)).addEvents(Collections.nCopies(10,
                    AddVehicleEvent.create(-1, VehicleDTO.builder().startPosition(new Point(5, 5)).build())));

    final RandomGenerator rng = new MersenneTwister(seed);
    for (int i = 0; i < 20; i++) {
        final long announceTime = rng.nextInt(DoubleMath.roundToInt(endTime * .8, RoundingMode.FLOOR));
        b.addEvent(AddParcelEvent.create(Parcel
                .builder(new Point(rng.nextDouble() * 10, rng.nextDouble() * 10),
                        new Point(rng.nextDouble() * 10, rng.nextDouble() * 10))
                .orderAnnounceTime(announceTime).pickupTimeWindow(TimeWindow.create(announceTime, endTime))
                .deliveryTimeWindow(TimeWindow.create(announceTime, endTime)).neededCapacity(0).buildDTO()));
    }

    b.addEvent(TimeOutEvent.create(endTime)).scenarioLength(endTime)
            .setStopCondition(StopConditions.limitedTime(endTime));

    return b.build();
}