Java - Write code to return human Readable Byte Count

Requirements

Write code to get human Readable Byte Count

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        long bytes = 42;
        System.out.println(humanReadableByteCount(bytes));
    }/* ww w .  jav a  2 s  .c  o  m*/

    public static final String humanReadableByteCount(long bytes) {
        if (bytes < 1024)
            return bytes + " B";
        int exp = (int) (Math.log(bytes) / Math.log(1024));
        String pre = "KMGTPE".charAt(exp - 1) + "iB";
        return String.format("%.1f %s", bytes / Math.pow(1024, exp), pre);
    }
}