Java Byte Value Format byteToHuman(long bytes, boolean si)

Here you can find the source of byteToHuman(long bytes, boolean si)

Description

byte To Human

License

Apache License

Declaration

public static String byteToHuman(long bytes, boolean si) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static String byteToHuman(long bytes, boolean si) {
        int unit = si ? 1000 : 1024;
        if (bytes < unit) {
            return bytes + " B";
        }//from  www.  ja  v a2s.co  m
        int exp = (int) (Math.log(bytes) / Math.log(unit));
        String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i");
        return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
    }

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

Related

  1. BytesToMB(long kb)
  2. bytesToMB(long sizeInBytes)
  3. bytesToMBString(long bytes)
  4. bytesToMegabytes(long bytes)
  5. bytesToMegaBytes(long bytes)
  6. byteToHumanreadable(long b)
  7. byteToMeg(long bytes)