Here you can find the source of humanReadableSize(long bytes)
Parameter | Description |
---|---|
bytes | Size in bytes. |
public static String humanReadableSize(long bytes)
//package com.java2s; /*// w w w. j av a2 s . co m * Copyright (C) 2011-2014 by Ahmed Osama el-Sawalhy * * The Modified MIT Licence (GPL v3 compatible) * Licence terms are in a separate file (LICENCE.md) * * Project/File: KeepUp/com.yagasoft.keepup/Util.java * * Modified: 25-Jun-2014 (02:54:57) * Using: Eclipse J-EE / JDK 8 / Windows 8.1 x64 */ public class Main { /** * Human readable size conversion.<br/> * <br /> * Credit: 'aioobe' at 'StackOverFlow.com' * * @param bytes * Size in bytes. * @return Human readable size. */ public static String humanReadableSize(long bytes) { int unit = 1024; if (bytes < unit) { return bytes + " B"; } int exp = (int) (Math.log(bytes) / Math.log(unit)); String pre = ("KMGTPE").charAt(exp - 1) + ("i"); return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre); } }