Here you can find the source of byteCountToDisplaySize(long size)
public static String byteCountToDisplaySize(long size)
//package com.java2s; //License from project: Apache License public class Main { /**/*from w w w .j ava 2s.co m*/ * The number of bytes in a kilobyte. */ public static final long ONE_KB = 1024; /** * The number of bytes in a megabyte. */ public static final long ONE_MB = ONE_KB * ONE_KB; /** * The number of bytes in a gigabyte. */ public static final long ONE_GB = ONE_KB * ONE_MB; public static String byteCountToDisplaySize(long size) { String displaySize; if (size / ONE_GB > 0) { displaySize = String.format("%1$.1f GB", (float) size / ONE_GB); } else if (size / ONE_MB > 0) { displaySize = String.format("%1$.1f MB", (float) size / ONE_MB); } else if (size / ONE_KB > 0) { displaySize = String.format("%1$.1f KB", (float) size / ONE_KB); } else { displaySize = String.valueOf(size) + " Bytes"; } return displaySize; } }