Here you can find the source of bytesToString(long bytes)
Parameter | Description |
---|---|
bytes | Numeric bytes (ex: 123899381) |
public static String bytesToString(long bytes)
//package com.java2s; //License from project: Open Source License import java.text.DecimalFormat; public class Main { /**//from w w w .j av a2 s. c om * Convert a number of bytes into a human readable string * @param bytes Numeric bytes (ex: 123899381) * @return Human readable version of bytes (ex: 1.4 GB) */ public static String bytesToString(long bytes) { if (bytes > 1024 * 1024 * 1024) { return new DecimalFormat("#.##").format((double) bytes / (1024.0 * 1024.0 * 1024.0)) + " GB"; } if (bytes > 1024 * 1024) { return new DecimalFormat("#.##").format((double) bytes / (1024.0 * 1024.0)) + " MB"; } if (bytes > 1024) { return new DecimalFormat("#.##").format((double) bytes / 1024.0) + " KB"; } return bytes + " B"; } }