Java Long Number Readable Format formatBytes(long l)

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

Description

Metodo para fazer a conversao de bytes

License

Open Source License

Parameter

Parameter Description
bytes a parameter

Return

bytes convertidos

Declaration

public static String formatBytes(long l) 

Method Source Code

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

public class Main {
    /**// w w w .  ja v a 2  s .  c  o  m
     * 
     * Metodo para fazer a conversao de bytes
     * 
     * @param bytes
     * @return bytes convertidos 
     */
    public static String formatBytes(long l) {

        if (l == 1L)
            return String.format("%d byte", new Object[] { Long.valueOf(l) });

        if (l < 1024L)
            return String.format("%d bytes", new Object[] { Long.valueOf(l) });

        if (l < 0x100000L && l % 1024L == 0L)
            return String.format("%.0f KB", new Object[] { Double.valueOf((double) l / 1024D) });

        if (l < 0x100000L)
            return String.format("%.1f KB", new Object[] { Double.valueOf((double) l / 1024D) });

        if (l % 0x100000L == 0L && l < 0x40000000L)
            return String.format("%.0f MB", new Object[] { Double.valueOf((double) l / 1048576D) });

        if (l < 0x40000000L)
            return String.format("%.1f MB", new Object[] { Double.valueOf((double) l / 1048576D) });

        if (l % 0x40000000L == 0L && l < 0x10000000000L)
            return String.format("%.0f GB", new Object[] { Double.valueOf((double) l / 1073741824D) });

        if (l < 0x10000000000L)
            return String.format("%.1f GB", new Object[] { Double.valueOf((double) l / 1073741824D) });

        if (l % 0x10000000000L == 0L && l < 0x4000000000000L)
            return String.format("%.0f TB", new Object[] { Double.valueOf((double) l / 1099511627776D) });

        if (l < 0x4000000000000L)
            return String.format("%.1f TB", new Object[] { Double.valueOf((double) l / 1099511627776D) });

        else
            return String.format("%d bytes", new Object[] { Long.valueOf(l) });

    }
}

Related

  1. formatBytes(long bytes)
  2. formatBytes(long bytes)
  3. formatBytes(long bytes)
  4. formatBytes(long bytes)
  5. formatBytes(long d)
  6. formatBytes(long size)
  7. getHumanReadable(long ts, boolean addUTC)
  8. getHumanReadableFileSize(Long fileSize)
  9. getHumanReadableIfSpeed(long ifSpeed)