Here you can find the source of formatSize(long size)
Parameter | Description |
---|---|
size | a parameter |
public static String formatSize(long size)
//package com.java2s; //License from project: LGPL import java.text.*; public class Main { public static String formatSize(Object size) { if (size instanceof Integer) { return formatSize(((Integer) size).intValue()); }/*from ww w.j av a2s. c om*/ if (size instanceof Long) { return formatSize(((Long) size).intValue()); } return null; } /** * @param size * @return String */ public static String formatSize(long size) { NumberFormat formatter = NumberFormat.getInstance(); formatter.setMaximumFractionDigits(1); formatter.setMinimumFractionDigits(1); float mbValue = size; String format; if (mbValue < 1024) { format = formatter.format(mbValue) + " KB"; } else { if (mbValue < 1048576) { mbValue /= 1024; format = formatter.format(mbValue) + " KB"; } else { if (mbValue < 109970456576L) { mbValue /= 1048576; format = formatter.format(mbValue) + " MB"; } else { formatter.setMaximumFractionDigits(1); formatter.setMinimumFractionDigits(1); mbValue /= (float) 1048576; format = formatter.format(mbValue) + " KB"; } } } return format; } }