Here you can find the source of formatBytes(long bytes)
Parameter | Description |
---|---|
bytes | bytes |
public static String formatBytes(long bytes)
//package com.java2s; //License from project: Open Source License import java.text.DecimalFormat; public class Main { /**/* w ww . j a v a2s. c om*/ * Format the bytes into readable text * * @param bytes * bytes * @return bytes text */ public static String formatBytes(long bytes) { double value = bytes; String unit = "B"; if (bytes >= 1024) { int exponent = (int) (Math.log(bytes) / Math.log(1024)); exponent = Math.min(exponent, 4); switch (exponent) { case 1: unit = "KB"; break; case 2: unit = "MB"; break; case 3: unit = "GB"; break; case 4: unit = "TB"; break; } value = bytes / Math.pow(1024, exponent); } DecimalFormat df = new DecimalFormat("#.##"); return df.format(value) + " " + unit; } }