Java - get file Size Text in readable string

Description

get file Size Text in readable string

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        long fileSize = 42;
        System.out.println(getSizeText(fileSize));
    }//from   w  w  w  . j a  v  a2  s . co m

    public static String getSizeText(long fileSize) {
        if (fileSize <= 0) {
            return "0.0M";
        }
        if (fileSize > 0 && fileSize < 100 * 1024) {

            return "0.1M";
        }
        float result = fileSize;
        String suffix = "M";
        result = result / 1024 / 1024;
        return String.format("%.1f", result) + suffix;
    }
}

get file Size Text in readable string

Related Exercise