Here you can find the source of toHumanReadableByteCount(final long bytes)
Parameter | Description |
---|---|
bytes | byte count, may be negative as well. |
public static String toHumanReadableByteCount(final long bytes)
//package com.java2s; //License from project: Apache License public class Main { private static final String[] BYTE_UNITS = { "B", "KiB", "MiB", "GiB" }; private static final long BYTE_FAC = 1024; /**/*from w w w. j a v a 2s.co m*/ * Returns a human-readable version of the given byte count. * * @param bytes byte count, may be negative as well. * @return Byte count in B, KiB, MiB or GiB - including the unit. */ public static String toHumanReadableByteCount(final long bytes) { double value = bytes; int i = 0; for (; Math.abs(value) > BYTE_FAC && i < BYTE_UNITS.length; ++i) { value /= BYTE_FAC; } return ((long) value) + " " + BYTE_UNITS[i]; } }