Example usage for java.text DecimalFormat setMaximumFractionDigits

List of usage examples for java.text DecimalFormat setMaximumFractionDigits

Introduction

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

Prototype

@Override
public void setMaximumFractionDigits(int newValue) 

Source Link

Document

Sets the maximum number of digits allowed in the fraction portion of a number.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DecimalFormat format = new DecimalFormat();

    format.setMaximumFractionDigits(4);

    System.out.println(format.format(123456789.12345678));

}

From source file:Main.java

public static String MybigdecimalToString(BigDecimal myBigD) {
    myBigD = myBigD.setScale(2);/*from   w w  w .ja  v  a2s. c  om*/
    DecimalFormat df = new DecimalFormat();
    df.setMaximumFractionDigits(2);
    df.setMinimumFractionDigits(0);
    return df.format(myBigD);
}

From source file:Main.java

public static String formatPercent(BigDecimal number) {
    String result = "0";

    if (number != null) {
        number = number.multiply(new BigDecimal("100"));
        DecimalFormat formatter = new DecimalFormat();
        formatter.setMaximumFractionDigits(2);
    }/* w  w  w  . j a  va  2 s . c  o  m*/

    return result;
}

From source file:Main.java

public static String byteToKB(int bt) {
    DecimalFormat df = (DecimalFormat) NumberFormat.getInstance();
    df.setMaximumFractionDigits(2);
    return df.format(bt / 1024.0) + " KB";
}

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 formatPercentWithFixedFractionDigit(BigDecimal number, int fractionDigit) {
    String result = "0";

    if (number != null) {
        number = number.multiply(new BigDecimal("100"));
        DecimalFormat formatter = new DecimalFormat();
        formatter.setMaximumFractionDigits(fractionDigit);
        formatter.setMinimumFractionDigits(fractionDigit);

        result = formatter.format(number);
    }// w  ww . ja v a  2  s .co  m
    return result;
}

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 ww. j  av a2 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

public static String converSizeToString(long size) {
    String ext[] = { "B", "KB", "MB", "G", "T", "P" };
    int i = 0;/* www . j  a  v a2  s.  co m*/
    long duration = size;
    while (duration >= 1024) {
        System.out.println("duration=" + duration + "  i=" + i);
        duration = duration >> 10;
        i++;
        System.out.println("duration=" + duration + "  i=" + i);
    }
    double d = Math.pow(2, 10 * (i));
    java.text.DecimalFormat df = new DecimalFormat();
    df.setMaximumFractionDigits(2);
    df.setMinimumFractionDigits(2);
    return df.format(size / d) + ext[i];
}

From source file:Main.java

public static String formatNum(double value) {
    String retValue = null;/*from  w  ww  .  ja v a2s.c o m*/
    DecimalFormat df = new DecimalFormat();
    df.setMinimumFractionDigits(0);
    df.setMaximumFractionDigits(2);
    retValue = df.format(value);
    retValue = retValue.replaceAll(",", "");
    return retValue;
}

From source file:Main.java

public static String formatCurrency(double amount, int precision, String pattern, Locale locale) {
    NumberFormat nf = NumberFormat.getCurrencyInstance(locale);
    DecimalFormat df = (DecimalFormat) nf;
    df.setMinimumFractionDigits(precision);
    df.setMaximumFractionDigits(precision);
    df.setDecimalSeparatorAlwaysShown(true);
    df.applyPattern(pattern);//from w w  w. j a v  a 2  s .  com
    return df.format(amount);
}