Here you can find the source of stringForBytes(long bytes)
Parameter | Description |
---|---|
bytes | the number of bytes |
public static String stringForBytes(long bytes)
//package com.java2s; //License from project: Apache License import java.text.*; public class Main { /**//from w w w . j ava2 s . c o m * Return a human-readable indication of a size, given a number of bytes. * @param bytes the number of bytes * @return a string such as (1.23GB, 1.4MB, etc.) */ public static String stringForBytes(long bytes) { DecimalFormat deci = new DecimalFormat("0.00"); double gigs = (((double) bytes / 1024d) / 1024d / 1024d); double megs = (((double) bytes / 1024d) / 1024d); double kb = ((double) bytes / 1024d); if (gigs > 1) { return deci.format(gigs) + " GB"; } if (megs > 1) { return deci.format(megs) + " MB"; } if (kb > 1) { return deci.format(kb) + " KB"; } return bytes + " bytes"; } }