private static DecimalFormat twoDecimalForm = new DecimalFormat(“#.##”);
private static final double BYTE = 1024, KB = BYTE, MB = KB*BYTE, GB = MB*BYTE;
public static String convertBytesToSuitableUnit(long bytes){
String bytesToSuitableUnit= bytes + ” B”;
if(bytes >= GB) {
double tempBytes = bytes/GB;
bytesToSuitableUnit = twoDecimalForm.format(tempBytes) + ” GB”;
return bytesToSuitableUnit;
}
if(bytes >= MB) {
double tempBytes = bytes/MB;
bytesToSuitableUnit = twoDecimalForm.format(tempBytes) + ” MB”;
return bytesToSuitableUnit;
}
if(bytes >= KB) {
double tempBytes = bytes/KB;
bytesToSuitableUnit = twoDecimalForm.format(tempBytes) + ” kB”;
return bytesToSuitableUnit;
}
return bytesToSuitableUnit;
}
private static final double BYTE = 1024, KB = BYTE, MB = KB*BYTE, GB = MB*BYTE;
public static String convertBytesToSuitableUnit(long bytes){
String bytesToSuitableUnit= bytes + ” B”;
if(bytes >= GB) {
double tempBytes = bytes/GB;
bytesToSuitableUnit = twoDecimalForm.format(tempBytes) + ” GB”;
return bytesToSuitableUnit;
}
if(bytes >= MB) {
double tempBytes = bytes/MB;
bytesToSuitableUnit = twoDecimalForm.format(tempBytes) + ” MB”;
return bytesToSuitableUnit;
}
if(bytes >= KB) {
double tempBytes = bytes/KB;
bytesToSuitableUnit = twoDecimalForm.format(tempBytes) + ” kB”;
return bytesToSuitableUnit;
}
return bytesToSuitableUnit;
}
No comments:
Post a Comment