Java Long Number Readable Format formatBytes(long bytes)

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

Description

format Bytes

License

Open Source License

Declaration

public static String formatBytes(long bytes) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    private static final int UNIT = 1024;
    private static final String B = "B";
    private static final String KB = "KB";
    private static final String MB = "MB";
    private static final String GB = "GB";
    private static final String TB = "TB";

    public static String formatBytes(long bytes) {
        double bytesDouble = bytes;
        int unitIndex = 0;
        String unit;//from w w w . java2  s  .co m
        while (bytesDouble >= UNIT) {
            unitIndex++;
            bytesDouble = Math.round(bytesDouble * 100 / UNIT) / 100.0;
        }
        switch (unitIndex) {
        case 0:
            unit = B;
            break;
        case 1:
            unit = KB;
            break;
        case 2:
            unit = MB;
            break;
        case 3:
            unit = GB;
            break;
        case 4:
            unit = TB;
            break;
        default:
            unit = "PB";
        }
        return bytesDouble + " " + unit;
    }
}

Related

  1. formatBytes(long bts)
  2. formatBytes(long bytes)
  3. formatBytes(long bytes)
  4. formatBytes(long bytes)
  5. formatBytes(long bytes)
  6. formatBytes(long bytes)
  7. formatBytes(long bytes)
  8. formatBytes(long bytes)
  9. formatBytes(long bytes)