Java Number Convert convertSize(Long size)

Here you can find the source of convertSize(Long size)

Description

convert Size

License

Open Source License

Declaration

public static String convertSize(Long size) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.math.BigDecimal;

public class Main {

    public static String convertSize(Long size) {
        String retStr = "";
        for (int i = 3; i > 0; i--) {
            double compSize = Math.pow(1024, i);
            double remainder = size / compSize;
            if (remainder > 1) {
                if (i == 3) {
                    retStr = round(remainder, 2, BigDecimal.ROUND_HALF_EVEN) + "G";
                    break;
                } else if (i == 2) {
                    retStr = round(remainder, 2, BigDecimal.ROUND_HALF_EVEN) + "M";
                    break;
                } else {
                    retStr = round(remainder, 2, BigDecimal.ROUND_HALF_EVEN) + "K";
                    break;
                }// w ww.j  a  v a2 s.c  om
            }
            retStr = round(remainder, 2, BigDecimal.ROUND_HALF_EVEN) + "K";
        }
        return retStr;
    }

    public static double round(double value, int scale, int roundingMode) {
        BigDecimal bd = new BigDecimal(value);
        bd = bd.setScale(scale, roundingMode);
        double retValue = bd.doubleValue();
        bd = null;
        return retValue;
    }
}

Related

  1. convertMB2GB(int valueMB)
  2. convertMinutesToHours(Long minutes)
  3. convertNanoDiff(long startNanos, long stopNanos, TimeUnit timeUnit)
  4. convertNullToOne(final Object value)
  5. convertScientificToStandard(double value)
  6. convertSizeG1DetailsToSizeG1(final String size, final char units)
  7. convertSizeToMB(long sizeInBytes)
  8. convertsToLong(double v)
  9. convertStringToObjectWrapper(String className, String value)