Monday, November 11, 2013

JAVA CODE TO CONVERT BYTE TO KB MB GB

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;
}

No comments:

Post a Comment