1. 程式人生 > >把long型別的資料轉換為KB,MB,G

把long型別的資料轉換為KB,MB,G

public String FormetFileSize(long file) {
        DecimalFormat df = new DecimalFormat("#.00");
        String fileSizeString = "";
        if (file < 1024) {
            fileSizeString = df.format((double) file) + "B";
        } else if (fileS < 1048576) {
            fileSizeString = df.format((double) file / 1024) + "K";
        } else if (fileS < 1073741824) {
            fileSizeString = df.format((double) file / 1048576) + "M";
        } else {
            fileSizeString = df.format((double) file / 1073741824) + "G";
        }
        return fileSizeString;
    }