Example usage for java.text DecimalFormat setRoundingMode

List of usage examples for java.text DecimalFormat setRoundingMode

Introduction

In this page you can find the example usage for java.text DecimalFormat setRoundingMode.

Prototype

@Override
public void setRoundingMode(RoundingMode roundingMode) 

Source Link

Document

Sets the java.math.RoundingMode used in this DecimalFormat.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DecimalFormat format = new DecimalFormat();
    format.setRoundingMode(RoundingMode.CEILING);
    System.out.println(format.format(123456789123.45678));

}

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:Main.java

public static String round(double value) {
    DecimalFormat decimalFormat = new DecimalFormat("0.00");
    decimalFormat.setRoundingMode(RoundingMode.HALF_UP);

    return decimalFormat.format(value);
}

From source file:Main.java

public static DecimalFormat createDecimalFormat(int minInt, int maxInt, int minFract, int maxFract,
        char separator, RoundingMode mode) {

    DecimalFormat format = (DecimalFormat) DecimalFormat.getNumberInstance();
    format.setRoundingMode(mode);

    format.setMaximumFractionDigits(maxFract);
    format.setMinimumFractionDigits(minFract);
    format.setMaximumIntegerDigits(maxInt);
    format.setMinimumIntegerDigits(minInt);
    DecimalFormatSymbols decimalSymbolComma = new DecimalFormatSymbols();
    decimalSymbolComma.setDecimalSeparator(separator);
    format.setDecimalFormatSymbols(decimalSymbolComma);
    format.setGroupingUsed(false);/*from  w ww. ja va 2s  .c om*/

    return format;
}

From source file:org.esupportail.papercut.domain.UserPapercutInfos.java

/**
 * Papercut WebService gives balance values like 1.9465000000000003 
 * Error of epsilon machine is suspected in Papercut !
 * -> to workaround, we simply here round the balance ... 
 * //from ww w .jav a  2s . com
 * Note that 5 decimals is the max in papercut today, but maybe one day it will be more so here we return with 10 max 
 * 
 * @param balance
 * @return
 */
private static String fixRoundPapercutError(String balance) {
    DecimalFormat df = new DecimalFormat("#.##########");
    df.setRoundingMode(RoundingMode.HALF_UP);
    return df.format(Double.valueOf(balance));
}

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  ava 2 s  .  co  m*/
    }
    DecimalFormat localDecimalFormat = new DecimalFormat();
    localDecimalFormat.setMaximumFractionDigits(1);
    localDecimalFormat.setGroupingSize(0);
    localDecimalFormat.setRoundingMode(RoundingMode.FLOOR);
    return localDecimalFormat.format(0.1D * i);
}

From source file:org.apache.niolex.commons.codec.IntegerUtil.java

/**
 * Format the size into string end with one letter. We will round the result
 * by half up, and keep two decimal places.
 *
 * @param size the size need to be formatted
 * @return the string representation/*  w ww. j a  v  a  2s. c  o m*/
 */
public static final String formatSize(double size) {
    DecimalFormat df = new DecimalFormat("#,###.##");
    df.setRoundingMode(RoundingMode.HALF_UP);
    if (size >= Const.T) {
        return df.format(size / Const.T) + "T";
    }
    if (size >= Const.G) {
        return df.format(size / Const.G) + "G";
    }
    if (size >= Const.M) {
        return df.format(size / Const.M) + "M";
    }
    if (size >= Const.K) {
        return df.format(size / Const.K) + "K";
    }
    return df.format(size);
}

From source file:org.codelibs.fess.taglib.FessFunctions.java

public static String formatNumber(final long value) {
    int ratio = 1;
    String unit = "";
    String format = "0.#";
    if (value < 1024) {
        format = "0";
    } else if (value < (1024 * 1024)) {
        ratio = 1024;//w w w . j  a  v a 2 s .  c o  m
        unit = "K";
    } else if (value < (1024 * 1024 * 1024)) {
        ratio = 1024 * 1024;
        unit = "M";
    } else {
        ratio = 1024 * 1024 * 1024;
        unit = "G";
    }
    final DecimalFormat df = new DecimalFormat(format + unit);
    df.setRoundingMode(RoundingMode.HALF_UP);
    return df.format((double) value / ratio);
}

From source file:com.reizes.shiva.utils.CommonUtil.java

/**
 * ?   (  ?)/*from   w ww . j  a  v a2  s . co  m*/
 * @param input
 * @return
 */
public static String decimalPointTwo(Float input) {
    if (input == null || input == 0) {
        return "0.0";
    }
    DecimalFormat df = new DecimalFormat("#.##");
    df.setRoundingMode(RoundingMode.FLOOR);
    return df.format(input);
}