Here you can find the source of toHumanReadableFileSize(long fileSize)
Parameter | Description |
---|---|
fileSize | a parameter |
public static String toHumanReadableFileSize(long fileSize)
//package com.java2s; /*/*from w w w . j a v a2 s .c om*/ * Copyright (C) 2016 Florian Frankenberger. * * This library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this library; if not, see <http://www.gnu.org/licenses/>. */ public class Main { private static final String[] FILE_SIZE_NAMES = { "Bytes", "KB", "MB", "GB" }; /** * transforms the given file size in bytes into a human readable * string like "15 MB" or "256 KB" * * @param fileSize * @return */ public static String toHumanReadableFileSize(long fileSize) { for (int i = FILE_SIZE_NAMES.length - 1; i > 0; --i) { float thisValue = (float) Math.pow(1024, i); if (fileSize / thisValue > 1) { return String.format("%3.2f %s", fileSize / thisValue, FILE_SIZE_NAMES[i]); } } return "0"; } }