Example usage for java.text DecimalFormat DecimalFormat

List of usage examples for java.text DecimalFormat DecimalFormat

Introduction

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

Prototype

public DecimalFormat(String pattern) 

Source Link

Document

Creates a DecimalFormat using the given pattern and the symbols for the default java.util.Locale.Category#FORMAT FORMAT locale.

Usage

From source file:Main.java

public static String formatDouble(double dblValue) {
    DecimalFormat df = new DecimalFormat("####.00");
    return df.format(dblValue);
}

From source file:Main.java

public static String calculateSizeToString(long size) {
    String str = "";
    DecimalFormat df = new DecimalFormat("#.00");
    if (size < 1024) {
        str = df.format(size) + "B";
    } else if (size < 1024 * 1024) {
        str = df.format(size / 1024) + "KB";
    } else if (size < 1024 * 1024 * 1024) {
        str = df.format(size / 1024 / 1024) + "M";
    } else {/*ww w .j  av  a  2  s.  c  o m*/
        str = df.format(size / 1024 / 1024 / 1024) + "G";
    }
    return str;
}

From source file:Main.java

public static String getVersion(int version, String ver) {
    NumberFormat formatter = new DecimalFormat("000");
    String number = ver + "." + formatter.format(version);
    return number.toLowerCase();
}

From source file:Main.java

public static String getTrafficData(long total) {

    String dataText = "0";
    DecimalFormat df = new DecimalFormat("###.00");
    if (total == -1) {
        return dataText;
    } else if (total < 10240) {
        dataText = "<10K";

    } else if (total < 1024 * 1024) {
        dataText = df.format(total / 1024f) + "KB";
    } else if (total < 1024 * 1024 * 1024) {
        dataText = df.format(total / 1024f / 1024f) + "MB";
    } else if (total < 1024 * 1024 * 1024 * 1024) {
        dataText = df.format(total / 1024f / 1024f / 1024f) + "GB";
    }//from w  ww  .j  a  va  2s.  c om
    return dataText;
}

From source file:Main.java

public static String formatFloat2Point(float num) {
    String f1 = "";
    try {/* w w w. ja va 2s .  com*/
        DecimalFormat fnum = new DecimalFormat("0.00");
        f1 = fnum.format(num);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return f1;
}

From source file:Main.java

public static String FormetFileSize(long fileSize) {
    DecimalFormat df = new DecimalFormat("0.0");
    String fileSizeString = "";
    if (fileSize < 1024L)
        fileSizeString = df.format(fileSize) + "B";
    else if (fileSize < 1048576L)
        fileSizeString = df.format(fileSize / 1024.0D) + "K";
    else if (fileSize < 1073741824L)
        fileSizeString = df.format(fileSize / 1048576.0D) + "M";
    else {//  w w w  .  ja v a2  s.  c  om
        fileSizeString = df.format(fileSize / 1073741824.0D) + "G";
    }
    return fileSizeString;
}

From source file:Main.java

public static String countBMI(float paramFloat1, float paramFloat2) {
    float f = paramFloat1 / (paramFloat2 * paramFloat2);
    return new DecimalFormat("######.0").format(f);
}

From source file:Main.java

public static String formatedIntStringFromInput(String inputStr) {
    Long n = longFromString(inputStr);
    DecimalFormat df = new DecimalFormat("#,###");
    return df.format(n);
}

From source file:Main.java

public static double roundTwoDecimals(double d) {
    try {//from ww  w  .  j av a2  s  . c  o  m
        DecimalFormat twoDForm = new DecimalFormat("#.##");
        return Double.valueOf(twoDForm.format(d));
    } catch (NumberFormatException nfe) {
        Log.d("nfe", nfe.toString());
        try {
            int i = (int) (d * 100);
            d = (i / 100);
            return d;
        } catch (NumberFormatException ne) {
            return d;
        }
    }
}

From source file:Main.java

public static String formatFileSizeToString(long fileLen) {
    DecimalFormat df = new DecimalFormat("#.00");
    String fileSizeString = "";
    if (fileLen < 1024) {
        fileSizeString = df.format((double) fileLen) + "B";
    } else if (fileLen < 1048576) {
        fileSizeString = df.format((double) fileLen / 1024) + "K";
    } else if (fileLen < 1073741824) {
        fileSizeString = df.format((double) fileLen / 1048576) + "M";
    } else {//w  ww . j  ava  2 s  .  c o m
        fileSizeString = df.format((double) fileLen / 1073741824) + "G";
    }
    return fileSizeString;
}