Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.math.BigDecimal;

public class Main {
    public static String getFormatSize(double size) {
        double kiloByte = size / 1024.0D;
        if (kiloByte < 1.0D) {
            return size + " B"; // Byte(s)
        }

        double megaByte = kiloByte / 1024.0D;
        if (megaByte < 1.0D) {
            BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));
            return result1.setScale(2, 4).toPlainString() + " KB";
        }

        double gigaByte = megaByte / 1024.0D;
        if (gigaByte < 1.0D) {
            BigDecimal result2 = new BigDecimal(Double.toString(megaByte));
            return result2.setScale(2, 4).toPlainString() + " MB";
        }

        double teraBytes = gigaByte / 1024.0D;
        if (teraBytes < 1.0D) {
            BigDecimal result3 = new BigDecimal(Double.toString(gigaByte));
            return result3.setScale(2, 4).toPlainString() + " GB";
        }
        BigDecimal result4 = new BigDecimal(teraBytes);
        return result4.setScale(2, 4).toPlainString() + " TB";
    }
}