Java Double Number Format format(double size, String type)

Here you can find the source of format(double size, String type)

Description

format

License

Apache License

Declaration

private static String format(double size, String type) 

Method Source Code

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

public class Main {
    private static String format(double size, String type) {
        int precision = 0;

        if (size * 1000 % 10 > 0) {
            precision = 3;//  www  . ja va  2  s.c om
        } else if (size * 100 % 10 > 0) {
            precision = 2;
        } else if (size * 10 % 10 > 0) {
            precision = 1;
        } else {
            precision = 0;
        }

        String formatStr = "%." + precision + "f";

        if ("KB".equals(type)) {
            return String.format(formatStr, (size)) + "KB";
        } else if ("MB".equals(type)) {
            return String.format(formatStr, (size)) + "MB";
        } else if ("GB".equals(type)) {
            return String.format(formatStr, (size)) + "GB";
        } else if ("TB".equals(type)) {
            return String.format(formatStr, (size)) + "TB";
        } else if ("PB".equals(type)) {
            return String.format(formatStr, (size)) + "PB";
        }
        return String.format(formatStr, (size)) + "B";
    }
}

Related

  1. format(double num, int n)
  2. format(double number)
  3. format(Double number)
  4. format(double number, int nums)
  5. format(double number, int precision)
  6. format(double val, int n, int w)
  7. format(double value, int decimalPlaces)
  8. format(double value, int digits)
  9. format(final double power)