Here you can find the source of formatSize(long size)
Parameter | Description |
---|---|
size | The size in bytes to be formatted. |
public static String formatSize(long size)
//package com.java2s; public class Main { /**// w w w. jav a 2 s . co m * Takes a size of a file or heap of memory in the form of a long and returns a formatted readable version in the * form of byte units. For example, 46 would become 46B, 1,024 would become 1KB, 1,048,576 would become 1MB, etc. * * @param size The size in bytes to be formatted. * @return A formatted string representing the byte size. */ public static String formatSize(long size) { if (size < 1024) { return size + " B"; } int exp = (int) (Math.log(size) / Math.log(1024)); return String.format("%.1f %sB", size / Math.pow(1024, exp), "KMGTPE".charAt(exp - 1)); } }