Here you can find the source of readableSize(long size)
Parameter | Description |
---|---|
size | a parameter |
public static String readableSize(long size)
//package com.java2s; //License from project: Open Source License import java.text.DecimalFormat; public class Main { /**//from ww w. j a v a 2s .c o m * Return human readable file size. This function is limited to exabyte. * Note: How to handle overflow or underflow of long? * Source: http://stackoverflow.com/questions/3263892/format-file-size-as-mb-gb-etc * * @param size * @return a human-readable display value (includes units - EB, PB, TB, GB, MB, KB or bytes). */ public static String readableSize(long size) { if (size <= 1) return size + " byte"; final String[] units = new String[] { "bytes", "KB", "MB", "GB", "TB", "PB", "EB" }; int digitGroups = (int) (Math.log10(size) / Math.log10(1024)); return new DecimalFormat("#,##0.##").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups]; } }