Here you can find the source of formatFileSize(long size)
Parameter | Description |
---|---|
size | size to format |
String
with the formatted size
public static String formatFileSize(long size)
//package com.java2s; import java.text.DecimalFormat; public class Main { /**//from w w w . java 2s .c om * Takes a string with a byte count and converts it into a "nice" representation of size. * <p/> * 124 b <br> * 34 KB <br> * 12 MB <br> * 2 GB * <p/> * Created on May 281, 2004 by Chuck May * @param size size to format * @return <code>String</code> with the formatted size */ public static String formatFileSize(long size) { if (size < 1024) return size + " B"; else if (size < 1024 * 1024) return new DecimalFormat("#.# KB").format((double) size / 1024); else if (size < 1024 * 1024 * 1024) return new DecimalFormat("#.# MB").format((double) size / 1024 / 1024); return new DecimalFormat("#.# GB").format((double) size / 1024 / 1024 / 1024); } }