Here you can find the source of formatBytes(long size)
Parameter | Description |
---|---|
size | the number of bytes |
public static String formatBytes(long size)
//package com.java2s; // it under the terms of the GNU General Public License as published by public class Main { public static final long ONE_KB = 1024; public static final long ONE_MB = ONE_KB * ONE_KB; public static final long ONE_GB = ONE_KB * ONE_MB; /**/*from w w w.j av a2s.c om*/ * Returns a human-readable version of the file size, where the input represents a specific number of bytes. * * @param size the number of bytes * @return a human-readable display value (includes units) */ public static String formatBytes(long size) { String display; if (size / ONE_GB > 0) { display = String.format("%.2f", (size + 0.0) / ONE_GB) + " GB"; } else if (size / ONE_MB > 0) { display = String.format("%.2f", (size + 0.0) / ONE_MB) + " MB"; } else if (size / ONE_KB > 0) { display = String.format("%.2f", (size + 0.0) / ONE_KB) + " KB"; } else { display = String.valueOf(size) + " bytes"; } return display; } }