Example usage for java.math RoundingMode DOWN

List of usage examples for java.math RoundingMode DOWN

Introduction

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

Prototype

RoundingMode DOWN

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

Click Source Link

Document

Rounding mode to round towards zero.

Usage

From source file:Main.java

public static Double toDecimal(Double value) {

    DecimalFormat df = new DecimalFormat("#.000000");
    df.setRoundingMode(RoundingMode.DOWN);

    return Double.parseDouble(df.format(value));
}

From source file:org.libreplan.business.reports.dtos.Util.java

public static BigDecimal getIntegerPart(BigDecimal value) {
    if (value == null) {
        return value;
    }//from  ww  w.ja  va 2 s.  co  m
    return value.setScale(2, RoundingMode.DOWN);
}

From source file:org.techytax.helper.AmountHelper.java

public static BigInteger roundDownToInteger(BigDecimal amount) {
    if (amount != null) {
        return amount.setScale(0, RoundingMode.DOWN).toBigInteger();
    } else {//from w w w  .j  a v  a  2  s  . c o  m
        return null;
    }
}

From source file:org.techytax.helper.AmountHelper.java

public static BigDecimal roundDown(BigDecimal amount) {
    if (amount != null) {
        return amount.setScale(0, RoundingMode.DOWN);
    } else {//from   ww w.j av  a  2s.  co m
        return null;
    }
}

From source file:org.fabrician.enabler.util.TimeUtil.java

/**
 * Format duration message in integral units of increasing coarse grains for display purpose
 * //from   w w  w  .  j  a v  a2 s . co  m
 * @param duration
 *            the duration in millisecs
 * @return a formatted duration string in secs, mins,hrs or weeks
 */
public static String formatDuration(long duration) {
    Validate.isTrue(duration >= 0, "Duration is negative", duration);
    if (duration < SEC_DURATION) {
        return duration + "ms";
    } else if (duration >= SEC_DURATION && duration < MIN_DURATION) {
        return formatDurationMsg(TimeUnit.MILLISECONDS.toSeconds(duration), "sec");
    } else if (duration >= MIN_DURATION && duration < HR_DURATION) {
        long num_secs = TimeUnit.MILLISECONDS.toSeconds(duration);
        long num_mins = LongMath.divide(num_secs, 60, RoundingMode.DOWN);
        long residue_secs = num_secs - (num_mins * 60);
        return formatDurationMsg(num_mins, "min", residue_secs, "sec");
    } else if (duration >= HR_DURATION && duration < DAY_DURATION) {
        long num_mins = TimeUnit.MILLISECONDS.toMinutes(duration);
        long num_hrs = LongMath.divide(num_mins, 60, RoundingMode.DOWN);
        long residue_mins = num_mins - (num_hrs * 60);
        return formatDurationMsg(num_hrs, "hr", residue_mins, "min");
    } else if (duration >= DAY_DURATION && duration < WEEK_DURATION) {
        long num_hrs = TimeUnit.MILLISECONDS.toHours(duration);
        long num_days = LongMath.divide(num_hrs, 24, RoundingMode.DOWN);
        long residue_hrs = num_hrs - (num_days * 24);
        return formatDurationMsg(num_days, "day", residue_hrs, "hr");
    } else {
        long num_days = TimeUnit.MILLISECONDS.toDays(duration);
        long num_weeks = LongMath.divide(num_days, 7, RoundingMode.DOWN);
        long residue_days = num_days - (num_weeks * 7);
        return formatDurationMsg(num_weeks, "week", residue_days, "day");
    }

}

From source file:ch.cyberduck.cli.TerminalStreamListener.java

private void increment() {
    final TransferProgress progress = meter.getStatus();
    if (System.currentTimeMillis() - timestamp.get() < 100L) {
        if (!progress.isComplete()) {
            return;
        }/*from  w  w w . j av  a2  s . c  o  m*/
    }
    try {
        lock.acquire();
        final BigDecimal fraction;
        if (progress.getTransferred() == 0L) {
            fraction = BigDecimal.ZERO;
        } else {
            fraction = new BigDecimal(progress.getTransferred()).divide(new BigDecimal(progress.getSize()), 1,
                    RoundingMode.DOWN);
        }
        console.printf("\r%s[",
                Ansi.ansi().saveCursorPosition().eraseLine(Ansi.Erase.ALL).restoreCursorPosition());
        int i = 0;
        for (; i <= (int) (fraction.doubleValue() * width); i++) {
            console.printf("\u25AE");
        }
        for (; i < width; i++) {
            console.printf(StringUtils.SPACE);
        }
        console.printf("] %s%s", progress.getProgress(), Ansi.ansi().reset());
        timestamp.set(System.currentTimeMillis());
    } catch (InterruptedException e) {
        //
    } finally {
        lock.release();
    }
}

From source file:com.lm.lic.manager.util.GenUtil.java

public static double roundToTwoDecimals(double d) {
    DecimalFormat twoDecimalFormatter = new DecimalFormat("#.##");
    twoDecimalFormatter.setRoundingMode(RoundingMode.DOWN);
    return Double.valueOf(twoDecimalFormatter.format(d));
}

From source file:org.trend.hgraph.mapreduce.pagerank.CalculatePageRankReducer.java

static boolean pageRankEquals(double src, double dest, int scale) {
    BigDecimal a = new BigDecimal(src);
    BigDecimal b = new BigDecimal(dest);
    a = a.setScale(scale, RoundingMode.DOWN);
    b = b.setScale(scale, RoundingMode.DOWN);
    return a.compareTo(b) == 0 ? true : false;
}

From source file:cherry.foundation.type.format.CustomNumberFormatAnnotationFormatterFactory.java

@Override
public Parser<Number> getParser(CustomNumberFormat annotation, Class<?> fieldType) {
    int value = adjust(annotation.value());
    final NumberFormatter formatter = (isNotEmpty(annotation.pattern())
            ? new NumberFormatter(annotation.pattern())
            : numberFormatter.get(value));
    final int scale = (annotation.scale() < 0 ? value : annotation.scale());
    return new Parser<Number>() {
        @Override//from w w w.ja  v a 2 s  . c o  m
        public Number parse(String text, Locale locale) throws ParseException {
            return NumberUtil.setScale(formatter.parse(text, locale), scale, RoundingMode.DOWN);
        }
    };
}

From source file:ccc.cli.StringToDecConverterUtil.java

/**
 *
 * Converts a number represented as text to a decimal number.
 *
 * @param textNumber text representation of a number
 * @return BigDecimal//from w  w  w .  j av a 2  s  .  c  om
 */
public BigDecimal convert(final String textNumber) {
    BigDecimal decNumber = null;
    if (textNumber != null && textNumber.matches(DECIMAL_PATTERN)) {
        // BigNumber(String) constructor does not allow commas
        decNumber = new BigDecimal(textNumber.replaceAll(",", ""));

        if (decNumber.precision() > MAX_PRECISION
                || decNumber.scale() < 0
                        && Math.abs(decNumber.scale()) + decNumber.precision() > MAX_PRECISION - MAX_SCALE
                || decNumber.compareTo(MAX_VALUE) == 1) {

            LOG.warn("Value is larger than maximum precision allowed " + "and was rejected.");
            return null;
        }

        if (decNumber.scale() > MAX_SCALE) {
            LOG.trace("Scale is bigger than maximum allowed: " + decNumber.scale() + ". Resetting scale.");
            decNumber = decNumber.setScale(MAX_SCALE, RoundingMode.DOWN);
        }
    }
    return decNumber;
}