Java Long Number Readable Format formatBytes(long bytes)

Here you can find the source of formatBytes(long bytes)

Description

format Bytes

License

BSD License

Declaration

public static String formatBytes(long bytes) 

Method Source Code

//package com.java2s;
/*//from  w w  w.  j a va  2  s.com
 * Copyright (c) 2008-2011 by Jan Stender, Bjoern Kolbeck,
 *               Zuse Institute Berlin
 *
 * Licensed under the BSD License, see LICENSE file for details.
 *
 */

public class Main {
    public static String formatBytes(long bytes) {

        double kb = bytes / 1024.0;
        double mb = bytes / (1024.0 * 1024.0);
        double gb = bytes / (1024.0 * 1024.0 * 1024.0);
        double tb = bytes / (1024.0 * 1024.0 * 1024.0 * 1024.0);

        if (tb >= 1.0) {
            return String.format("%.2f TB", tb);
        } else if (gb >= 1.0) {
            return String.format("%.2f GB", gb);
        } else if (mb >= 1.0) {
            return String.format("%.2f MB", mb);
        } else if (kb >= 1.0) {
            return String.format("%.2f kB", kb);
        } else {
            return bytes + " bytes";
        }
    }
}

Related

  1. convertByteUnit(Long l)
  2. convertHumanSize(long byteSize)
  3. convertLongToMega(long value)
  4. convertMbIntoGb(long megaBytes)
  5. formatBytes(long bts)
  6. formatBytes(long bytes)
  7. formatBytes(long bytes)
  8. formatBytes(long bytes)
  9. formatBytes(long bytes)