Example usage for java.text DecimalFormat format

List of usage examples for java.text DecimalFormat format

Introduction

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

Prototype

public final String format(double number) 

Source Link

Document

Specialization of format.

Usage

From source file:Main.java

public static String converSizeToString(long size) {
    String ext[] = { "B", "KB", "MB", "G", "T", "P" };
    int i = 0;//  w w w .  j a v a  2 s .  c  om
    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 getFileSize(long size) {
    if (size <= 0) {
        return "0";
    }/*from   w  w  w.j  a  va2  s  . c  om*/
    java.text.DecimalFormat df = new DecimalFormat("##.##");
    float temp = (float) size / 1024;
    if (temp >= 1024) {
        return df.format(temp / 1024) + "M";
    } else {
        return df.format(temp) + "K";
    }
}

From source file:Main.java

public static String formatFileSizeToString(long fileLen) {
    DecimalFormat df = new DecimalFormat("0.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  w w.j a  va 2s  .  com*/
        fileSizeString = df.format((double) fileLen / 1073741824) + "G";
    }
    return fileSizeString;
}

From source file:Main.java

/**
 * Convert filesize unit//w  ww.j a va2 s.  c  om
 * 
 * @param size
 * @return
 */
public static String formetFileSize(long size) {
    DecimalFormat df = new DecimalFormat("#0.00");
    String fileSizeString = "";
    if (size < 1024) {
        fileSizeString = df.format((double) size) + "B";
    } else if (size < 1048576) {
        fileSizeString = df.format((double) size / 1024) + "K";
    } else if (size < 1073741824) {
        fileSizeString = df.format((double) size / 1048576) + "M";
    } else {
        fileSizeString = df.format((double) size / 1073741824) + "G";
    }
    return fileSizeString;
}

From source file:Main.java

public static String getFileSize(long size) {
    if (size <= 0)
        return "0";
    java.text.DecimalFormat df = new java.text.DecimalFormat("##.##");
    float temp = (float) size / 1024;
    if (temp >= 1024) {
        return df.format(temp / 1024) + "M";
    } else {//from  w w w.  j  a  v  a2  s . com
        return df.format(temp) + "K";
    }
}

From source file:Main.java

public static String getCurrentTime() {
    DecimalFormat df = new DecimalFormat("00");
    Calendar c = Calendar.getInstance();
    String currentTime = c.get(Calendar.YEAR)// + "-"
            + df.format((c.get(Calendar.MONTH) + 1)) // + "-"
            + df.format(c.get(Calendar.DAY_OF_MONTH))// + "-"
            + df.format(c.get(Calendar.HOUR_OF_DAY)) // + "-"
            + df.format(c.get(Calendar.MINUTE))// + "-"
            + df.format(c.get(Calendar.SECOND));
    return currentTime;
}

From source file:com.carlomicieli.jtrains.value.objects.Length.java

private static double round(double v) {
    DecimalFormat df = new DecimalFormat("#.##");
    return Double.valueOf(df.format(v));
}

From source file:Main.java

public static String convertByte(long size) {
    DecimalFormat df = new DecimalFormat("###.##");
    float f;/*from  w w  w  .  j  a  v  a2 s . c o m*/
    if (size < 1024 * 1024) {
        f = (float) ((float) size / (float) 1024);
        return (df.format(new Float(f).doubleValue()) + "KB");
    } else {
        f = (float) ((float) size / (float) (1024 * 1024));
        return (df.format(new Float(f).doubleValue()) + "MB");
    }
}

From source file:Main.java

public static String formatFileSize(long fileS) {
    java.text.DecimalFormat df = new java.text.DecimalFormat("#.00");
    String fileSizeString = "";
    if (fileS < 1024) {
        fileSizeString = df.format((double) fileS) + "B";
    } else if (fileS < 1048576) {
        fileSizeString = df.format((double) fileS / 1024) + "KB";
    } else if (fileS < 1073741824) {
        fileSizeString = df.format((double) fileS / 1048576) + "MB";
    } else {//from   w  w  w .  j  a v a2 s  . co  m
        fileSizeString = df.format((double) fileS / 1073741824) + "G";
    }
    return fileSizeString;
}

From source file:Main.java

public static String formatFileSize(long fileS) {
    String fileSizeString = "";
    if (fileS <= 0) {
        return "0 KB";
    }/*from ww  w  .  j  a va2  s.c o  m*/
    if (fileS < 1024) {
        fileSizeString = "1 KB";
    } else if (fileS < 1048576) {
        long inte = fileS / 1024;
        if (inte > 100) {
            DecimalFormat df = new DecimalFormat("##0");
            fileSizeString = df.format(Rounding((double) fileS / 1024)) + " KB";
        } else if (inte > 10) {
            DecimalFormat df = new DecimalFormat("##0.0");
            fileSizeString = df.format(Rounding((double) fileS / 1024)) + " KB";
        } else {
            DecimalFormat df = new DecimalFormat("##0.00");
            fileSizeString = df.format(Rounding((double) fileS / 1024)) + " KB";
        }
    } else if (fileS < 1073741824) {
        long inte = fileS / 1048576;
        if (inte > 100) {
            DecimalFormat df = new DecimalFormat("##0");
            fileSizeString = df.format(Rounding((double) fileS / 1048576)) + " MB";
        } else if (inte > 10) {
            DecimalFormat df = new DecimalFormat("##0.0");
            fileSizeString = df.format(Rounding((double) fileS / 1048576)) + " MB";
        } else {
            DecimalFormat df = new DecimalFormat("##0.00");
            fileSizeString = df.format(Rounding((double) fileS / 1048576)) + " MB";
        }
    } else if (fileS < 1099511627776L) {
        long inte = fileS / 1073741824;
        if (inte > 100) {
            DecimalFormat df = new DecimalFormat("##0");
            fileSizeString = df.format(Rounding((double) fileS / 1073741824)) + " GB";
        } else if (inte > 10) {
            DecimalFormat df = new DecimalFormat("##0.0");
            fileSizeString = df.format(Rounding((double) fileS / 1073741824)) + " GB";
        } else {
            DecimalFormat df = new DecimalFormat("##0.0");
            fileSizeString = df.format(Rounding((double) fileS / 1073741824)) + " GB";
        }
    } else {
        long inte = fileS / 1099511627776L;
        if (inte > 100) {
            DecimalFormat df = new DecimalFormat("##0");
            fileSizeString = df.format((double) fileS / 1099511627776L) + " TB";
        } else if (inte > 10) {
            DecimalFormat df = new DecimalFormat("##0.0");
            fileSizeString = df.format((double) fileS / 1099511627776L) + " TB";
        } else {
            DecimalFormat df = new DecimalFormat("##0.00");
            fileSizeString = df.format((double) fileS / 1099511627776L) + " TB";
        }
    }
    return fileSizeString;
}