Here you can find the source of formatFilesize(int s)
Parameter | Description |
---|---|
s | file size |
public static String formatFilesize(int s)
//package com.java2s; //License from project: Open Source License import java.text.NumberFormat; public class Main { /**/*from w w w . j a va 2 s. c o m*/ * Format file size * @param s file size * @return file size as string */ public static String formatFilesize(int s) { if (s < 1024) { return s + "B"; } else { NumberFormat nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(1); nf.setMinimumFractionDigits(1); if (s < (1024 * 1024)) { return nf.format((double) s / 1024) + "KB"; } else if (s < (1024 * 1024 * 1024)) { return nf.format((double) s / 1024 / 1024) + "MB"; } else if (s < (1024 * 1024 * 1024 * 1024)) { return nf.format((double) s / 1024 / 1024 / 1024) + "GB"; } else { return nf.format((double) s / 1024 / 1024 / 1024 / 1024) + "TB"; } } } }