Java Decimal Format getSpaceMessage(final double bytes)

Here you can find the source of getSpaceMessage(final double bytes)

Description

get Space Message

License

Open Source License

Declaration

public static String getSpaceMessage(final double bytes) 

Method Source Code


//package com.java2s;
import java.text.DecimalFormat;

public class Main {
    private static final long KILO = 1024;
    private static final long MEGA = KILO * KILO;
    private static final long GIGA = KILO * MEGA;
    private static final long TERA = KILO * GIGA;
    private static final DecimalFormat SPACE_FORMAT = new DecimalFormat("##0.#");

    public static String getSpaceMessage(final double bytes) {
        if (bytes < (KILO * 0.8)) {
            return bytes + "B";
        }//from w  w w .j ava  2s.  c o  m

        if (bytes < (MEGA * 0.8)) {
            final double kilos = bytes / KILO;
            return SPACE_FORMAT.format(kilos) + "kB";
        }

        if (bytes < (GIGA * 0.8)) {
            final double megas = bytes / MEGA;
            return SPACE_FORMAT.format(megas) + "MB";
        }

        if (bytes < (TERA * 0.8)) {
            final double gigas = bytes / GIGA;
            return SPACE_FORMAT.format(gigas) + "GB";
        }

        final double teras = bytes / TERA;
        return SPACE_FORMAT.format(teras) + "TB";
    }
}

Related

  1. getIntNum(Double value)
  2. getProgressExact(double balance, double cost)
  3. getRealEye(double eye)
  4. getRedondeoHaciaArriba(double pTotal)
  5. getScientificNotation(double value)
  6. getStandardDeviationString(double[] standardDeviationDoubles)
  7. getStandardDouble(double d, int pL)
  8. getString(Double d)
  9. getStringFromDouble(double number)