Here you can find the source of humanReadableBytes(long bytes)
Parameter | Description |
---|---|
bytes | the number of bytes |
public static String humanReadableBytes(long bytes)
//package com.java2s; //License from project: Open Source License public class Main { /**//from w w w . j av a2s . c om * Get bytes in human readable form. * @param bytes the number of bytes * @return the human readable string representation */ public static String humanReadableBytes(long bytes) { int unit = 1024; if (bytes < unit) { return bytes + " B"; } else { int exp = (int) (Math.log(bytes) / Math.log(unit)); return String.format("%.1f %sB", bytes / Math.pow(unit, exp), "KMGTPE".charAt(exp - 1)); } } }