Java Double Number Readable Format humanBytes(double bytes)

Here you can find the source of humanBytes(double bytes)

Description

humanBytes formats bytes as a human readable string.

License

Apache License

Parameter

Parameter Description
bytes the number of bytes

Return

a string representing the number of bytes in human readable string

Declaration

public static String humanBytes(double bytes) 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

public class Main {
    /**/*from  w w w.  java2s. com*/
     * humanBytes formats bytes as a human readable string.
     *
     * @param bytes the number of bytes
     * @return a string representing the number of bytes in human readable string
     */
    public static String humanBytes(double bytes) {
        int base = 1024;
        String[] pre = new String[] { "k", "m", "g", "t", "p", "e" };
        String post = "b";
        if (bytes < (long) base) {
            return String.format("%.2f b", bytes);
        }
        int exp = (int) (Math.log(bytes) / Math.log(base));
        int index = exp - 1;
        String units = pre[index] + post;
        return String.format("%.2f %s", bytes / Math.pow((double) base, (double) exp), units);
    }
}

Related

  1. humanBytes(double value)
  2. humanReadableNumber(double n)
  3. humanReadableUnits(double bytes, boolean internationalSystemOfUnits)
  4. humanSize(double size)