format File Size - Android File Input Output

Android examples for File Input Output:File Size

Description

format File Size

Demo Code


//package com.java2s;

public class Main {
    public static String formatFileSize(long size) {
        if (size < 1024) {
            return String.format("%d B", size);
        } else if (size < 1024 * 1024) {
            return String.format("%.1f KB", size / 1024.0f);
        } else if (size < 1024 * 1024 * 1024) {
            return String.format("%.1f MB", size / 1024.0f / 1024.0f);
        } else {/*www  .  j av a 2s.  c  o  m*/
            return String.format("%.1f GB",
                    size / 1024.0f / 1024.0f / 1024.0f);
        }
    }
}

Related Tutorials