Here you can find the source of formatSize(Long size)
Parameter | Description |
---|---|
size | the file size in bytes |
public static String formatSize(Long size)
//package com.java2s; //License from project: Open Source License public class Main { /**/*from w w w. j a v a 2 s . com*/ * Formats a file size. * @param size the file size in bytes * @return formatted file size, e.g. 1 KB, 2 MB, etc */ public static String formatSize(Long size) { if (size == null) { return null; } // Based on code by aioobe, http://stackoverflow.com/a/3758880 int unit = 1024; if (size < unit) { return String.format("%d B", size); } int exp = (int) (Math.log(size) / Math.log(unit)); return String.format("%.2f %cB", size / Math.pow(unit, exp), "KMGTPE".charAt(exp - 1)); } }