应用场景:
根据需求计算文件大小统一输出
起因:纯属无聊
废话不多直接上代码
public class FileUnitConstant {
public static final String GB = "GB";
public static final String MB = "MB";
public static final String KB = "KB";
}
import java.text.DecimalFormat;
import FileUnitConstant;
public class FileComputeUtils {
public static String computeFileSize(long size, String sizeUnit) {
long kb = 1024;
long mb = kb * 1024;
long gb = mb * 1024;
String fileSize = "";
DecimalFormat df = new DecimalFormat("0.00");
switch (sizeUnit) {
case FileUnitConstant.GB:
fileSize = df.format(size / (gb * 1.0));
break;
case FileUnitConstant.MB:
fileSize = df.format(size / (mb * 1.0));
break;
case FileUnitConstant.KB:
fileSize = df.format(size / (kb * 1.0));
break;
default:
fileSize = "0";
}
return fileSize;
}
}
完事