Infolinks In Text Ads

Jun 22, 2010

how to converts a byte size in a kbytes or Mbytes size ?

Convert byte to kbyte or Mbytes

static String convertFileSize(long size) {
        int divisor = 1;
        String unit = "bytes";
        if (size >= 1024 * 1024) {
            divisor = 1024 * 1024;
            unit = "MB";
        }
        else if (size >= 1024) {
            divisor = 1024;
            unit = "KB";
        }
        if (divisor == 1) return size / divisor + " " + unit;
        String aftercomma = "" + 100 * (size % divisor) / divisor;
        if (aftercomma.length() == 1) aftercomma = "0" + aftercomma;
        return size / divisor + "." + aftercomma + " " + unit;
    }

No comments:

Post a Comment

LinkWithin

Related Posts Plugin for WordPress, Blogger...