Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.math.BigDecimal;
import java.math.RoundingMode;

import java.text.DecimalFormat;

public class Main {
    public static String formatFileSize(long fileS) {
        String fileSizeString = "";
        if (fileS <= 0) {
            return "0 KB";
        }
        if (fileS < 1024) {
            fileSizeString = "1 KB";
        } else if (fileS < 1048576) {
            long inte = fileS / 1024;
            if (inte > 100) {
                DecimalFormat df = new DecimalFormat("##0");
                fileSizeString = df.format(Rounding((double) fileS / 1024)) + " KB";
            } else if (inte > 10) {
                DecimalFormat df = new DecimalFormat("##0.0");
                fileSizeString = df.format(Rounding((double) fileS / 1024)) + " KB";
            } else {
                DecimalFormat df = new DecimalFormat("##0.00");
                fileSizeString = df.format(Rounding((double) fileS / 1024)) + " KB";
            }
        } else if (fileS < 1073741824) {
            long inte = fileS / 1048576;
            if (inte > 100) {
                DecimalFormat df = new DecimalFormat("##0");
                fileSizeString = df.format(Rounding((double) fileS / 1048576)) + " MB";
            } else if (inte > 10) {
                DecimalFormat df = new DecimalFormat("##0.0");
                fileSizeString = df.format(Rounding((double) fileS / 1048576)) + " MB";
            } else {
                DecimalFormat df = new DecimalFormat("##0.00");
                fileSizeString = df.format(Rounding((double) fileS / 1048576)) + " MB";
            }
        } else if (fileS < 1099511627776L) {
            long inte = fileS / 1073741824;
            if (inte > 100) {
                DecimalFormat df = new DecimalFormat("##0");
                fileSizeString = df.format(Rounding((double) fileS / 1073741824)) + " GB";
            } else if (inte > 10) {
                DecimalFormat df = new DecimalFormat("##0.0");
                fileSizeString = df.format(Rounding((double) fileS / 1073741824)) + " GB";
            } else {
                DecimalFormat df = new DecimalFormat("##0.0");
                fileSizeString = df.format(Rounding((double) fileS / 1073741824)) + " GB";
            }
        } else {
            long inte = fileS / 1099511627776L;
            if (inte > 100) {
                DecimalFormat df = new DecimalFormat("##0");
                fileSizeString = df.format((double) fileS / 1099511627776L) + " TB";
            } else if (inte > 10) {
                DecimalFormat df = new DecimalFormat("##0.0");
                fileSizeString = df.format((double) fileS / 1099511627776L) + " TB";
            } else {
                DecimalFormat df = new DecimalFormat("##0.00");
                fileSizeString = df.format((double) fileS / 1099511627776L) + " TB";
            }
        }
        return fileSizeString;
    }

    public static double Rounding(double d) {
        BigDecimal bd = new BigDecimal(d);
        bd.setScale(1, RoundingMode.HALF_UP);
        return bd.floatValue();
    }
}