Java Money Format toMemoryUnits(double value)

Here you can find the source of toMemoryUnits(double value)

Description

to Memory Units

License

Apache License

Declaration

public static String toMemoryUnits(double value) 

Method Source Code

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

public class Main {
    public static String toMemoryUnits(double value) {
        if (value == 0) {
            return "0";
        }//w ww.  java2 s.  c  o m
        if (value < (10l << 10)) {
            String val = String.format("%.2f", value).trim();
            if (val.endsWith(".00")) {
                val = val.substring(0, val.length() - 3);
            } else if (val.indexOf('.') > 0 && val.endsWith("0")) {
                val = val.substring(0, val.length() - 1);
            }
            return val;
        } else if (value < (1l << 20)) {
            return String.format("%5.4gKi", value / (1l << 10)).trim();
        } else if (value < (1l << 30)) {
            return String.format("%5.4gMi", value / (1l << 20)).trim();
        } else if (value < (1l << 40)) {
            return String.format("%5.4gGi", value / (1l << 30)).trim();
        } else {
            return String.format("%dGi", (long) (value / (1l << 30))).trim();
        }
    }
}

Related

  1. getMoneyString(double money)
  2. hourAndMinutesToString(int hours, int minutes)
  3. moneyDelFormat(String s)
  4. str2money(String str)
  5. toMemoryString(double bytes)
  6. toMoney(long amount)