Java BigDecimal Format formatFileSize(double size)

Here you can find the source of formatFileSize(double size)

Description

format File Size

License

Apache License

Declaration

public static String formatFileSize(double size) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.math.BigDecimal;

public class Main {
    public static String formatFileSize(double size) {
        double kiloByte = size / 1024;
        if (kiloByte < 1) {
            return size + "Byte(s)";
        }//from  www.  j  a  v  a2s.  co  m

        double megaByte = kiloByte / 1024;
        if (megaByte < 1) {
            BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));
            return result1.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "KB";
        }

        double gigaByte = megaByte / 1024;
        if (gigaByte < 1) {
            BigDecimal result2 = new BigDecimal(Double.toString(megaByte));
            return result2.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "MB";
        }

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

Related

  1. formatDouble(double value, int decimalPlaces)
  2. formatDoubleNumber(double f)
  3. formatDoubleValue(int medianAfterTheDecimalPoint, String doubleStringValue)
  4. formate(BigDecimal amount)
  5. formatFAAssertRatioWithoutPercent(BigDecimal value)
  6. formatFloat0(String value, int n)
  7. formatForPercentage(BigDecimal value)
  8. formatManey(BigDecimal date)
  9. formatMoney(BigDecimal money, int scale, double divisor)