Java Long Number Readable Format humanReadableSize(long bytes)

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

Description

Human readable size conversion.

Credit: 'aioobe' at 'StackOverFlow.com'

License

MIT License

Parameter

Parameter Description
bytes Size in bytes.

Return

Human readable size.

Declaration

public static String humanReadableSize(long bytes) 

Method Source Code

//package com.java2s;
/*//  w w w. j av  a2 s  .  co m
 * Copyright (C) 2011-2014 by Ahmed Osama el-Sawalhy
 *
 *      The Modified MIT Licence (GPL v3 compatible)
 *          Licence terms are in a separate file (LICENCE.md)
 *
 *      Project/File: KeepUp/com.yagasoft.keepup/Util.java
 *
 *         Modified: 25-Jun-2014 (02:54:57)
 *            Using: Eclipse J-EE / JDK 8 / Windows 8.1 x64
 */

public class Main {
    /**
     * Human readable size conversion.<br/>
     * <br />
     * Credit: 'aioobe' at 'StackOverFlow.com'
     *
     * @param bytes
     *            Size in bytes.
     * @return Human readable size.
     */
    public static String humanReadableSize(long bytes) {
        int unit = 1024;

        if (bytes < unit) {
            return bytes + " B";
        }

        int exp = (int) (Math.log(bytes) / Math.log(unit));
        String pre = ("KMGTPE").charAt(exp - 1) + ("i");

        return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
    }
}

Related

  1. humanReadableDuration(long length)
  2. humanReadableDuration(long ms)
  3. humanReadableInt(long number)
  4. humanReadableNumber(long total)
  5. humanReadableSize(long byteCount)
  6. humanReadableTime(final long duration)
  7. humanTimeDiff(long timeDiff)
  8. readable(long bytes)
  9. stringForBytes(long bytes)