import java.text.DecimalFormat;
public class ByteCal {
public String byteCalculation(String bytes) {
String retFormat = "0";
Double size = Double.parseDouble(bytes);
String[] s = { "bytes", "KB", "MB", "GB", "TB", "PB" };
if (bytes != "0") {
int idx = (int) Math.floor(Math.log(size) / Math.log(1024));
DecimalFormat df = new DecimalFormat("#,###.##");
double ret = ((size / Math.pow(1024, Math.floor(idx))));
retFormat = df.format(ret) + " " + s[idx];
} else {
retFormat += " " + s[0];
}
return retFormat;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ByteCal bc = new ByteCal();
System.out.println(bc.byteCalculation("1059000"));
}
}
결과
1.01 MB728x90
반응형
'Language > Java' 카테고리의 다른 글
| des 암복호화 (0) | 2018.01.15 |
|---|---|
| 대용량 엑셀다운로드 (0) | 2018.01.13 |
| 자바 숫자 체크 (0) | 2018.01.13 |
| Math.random() 취약점 대체 (0) | 2018.01.13 |
| AES128 암복호화 모듈 (0) | 2018.01.11 |