目录
- 1 实现
- 2 实现
1 实现
将file 文件大小变成有单位的
代码语言:javascript复制public static String changeFileFormat(String flow) {
BigDecimal flows = new BigDecimal(flow);
if (flows.compareTo(new BigDecimal(0)) > 0 && flows.compareTo(new BigDecimal(1024)) < 0) {//小于1M
return flows.toString() "B";
} else if(flows.compareTo(new BigDecimal(1024)) >= 0 && flows.compareTo(new BigDecimal(1048576)) < 0){
BigDecimal result = flows.divide(new BigDecimal(1024),2,BigDecimal.ROUND_HALF_UP);
return result.toString() "KB";
} else if(flows.compareTo(new BigDecimal(1048576)) >= 0 && flows.compareTo(new BigDecimal(1073741824)) < 0){
BigDecimal result = flows.divide(new BigDecimal(1048576),2,BigDecimal.ROUND_HALF_UP);
return result.toString() "MB";
} else if(flows.compareTo(new BigDecimal(1073741824)) >= 0 && flows.compareTo(new BigDecimal("1099511627776")) < 0){
BigDecimal result = flows.divide(new BigDecimal(1073741824),2,BigDecimal.ROUND_HALF_UP);
return result.toString() "GB";
} else if(flows.compareTo(new BigDecimal("1099511627776")) >= 0 && flows.compareTo(new BigDecimal("1125899906842624")) < 0){
BigDecimal result = flows.divide(new BigDecimal("1099511627776"),2,BigDecimal.ROUND_HALF_UP);
return result.toString() "TB";
} else if(flows.compareTo(new BigDecimal("1125899906842624")) >= 0){
BigDecimal result = flows.divide(new BigDecimal("1125899906842624"),2,BigDecimal.ROUND_HALF_UP);
return result.toString() "PB";
}else {
return "0";
}
}
2 实现
代码语言:javascript复制/**
* 将xxZB,xxPB,xxTB,xxGB,xxMB转为 xxKB
* @param flow xxGB,xxMB
*/
private static String changeFileToKBInfo(String flow) {
if(flow.contains("KB")){
return flow.replace("KB","");
}else if(flow.contains("MB")){
BigDecimal flows = new BigDecimal(flow.replace("MB",""));
BigDecimal result = flows.multiply(new BigDecimal(1024)).setScale(2,BigDecimal.ROUND_HALF_UP);
return result.toString();
}else if(flow.contains("GB")){
BigDecimal flows = new BigDecimal(flow.replace("GB",""));
BigDecimal result = flows.multiply(new BigDecimal(1048576)).setScale(2,BigDecimal.ROUND_HALF_UP);
return result.toString();
}else if(flow.contains("TB")){
BigDecimal flows = new BigDecimal(flow.replace("TB",""));
BigDecimal result = flows.multiply(new BigDecimal(1073741824)).setScale(2,BigDecimal.ROUND_HALF_UP);
return result.toString();
}
else if(flow.contains("PB")){
BigDecimal flows = new BigDecimal(flow.replace("PB",""));
BigDecimal result = flows.multiply(new BigDecimal("1099511627776")).setScale(2,BigDecimal.ROUND_HALF_UP);
return result.toString();
}
else if(flow.contains("EB")){
BigDecimal flows = new BigDecimal(flow.replace("EB",""));
BigDecimal result = flows.multiply(new BigDecimal("1125899906842624")).setScale(2,BigDecimal.ROUND_HALF_UP);
return result.toString();
}
else {
return "0";
}
}