Here you can find the source of formatFileSize(long fileSize, int decimalPos)
Parameter | Description |
---|---|
fileSize | the size of the file |
decimalPos | the number iof decimal places |
public static String formatFileSize(long fileSize, int decimalPos)
//package com.java2s; import java.text.NumberFormat; public class Main { /**/*from www . ja v a2s . com*/ * */ private static final int BYTES_IN_KILOBYTE = 1024; /** * Formats a file size in hmnan readable form. * @param fileSize the size of the file * @param decimalPos the number iof decimal places * @return a formatted file size. */ public static String formatFileSize(long fileSize, int decimalPos) { NumberFormat fmt = NumberFormat.getNumberInstance(); if (decimalPos >= 0) { fmt.setMaximumFractionDigits(decimalPos); } String formattedSize; final double size = fileSize; double val = size / (BYTES_IN_KILOBYTE * BYTES_IN_KILOBYTE * BYTES_IN_KILOBYTE); if (val > 1) { formattedSize = fmt.format(val).concat(" GB"); } else { val = size / (BYTES_IN_KILOBYTE * BYTES_IN_KILOBYTE); if (val > 1) { formattedSize = fmt.format(val).concat(" MB"); } else { val = size / BYTES_IN_KILOBYTE; if (val > 1) { formattedSize = fmt.format(val).concat(" KB"); } else { formattedSize = fmt.format(size).concat(" bytes"); } } } return formattedSize; } }