Java Number Format Pattern format(long bytes)

Here you can find the source of format(long bytes)

Description

format

License

Apache License

Declaration

private static String format(long bytes) 

Method Source Code

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

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class Main {
    private static final NumberFormat MEM_FMT = new DecimalFormat("##,###.##");

    private static String format(long bytes) {
        double val = bytes;
        int mag = 0;
        while (val > 1024) {
            val = val / 1024;
            mag++;/*from  w  w w  .  j  av a2s.  com*/
        }

        String formatted = MEM_FMT.format(val);
        switch (mag) {
        case 0:
            return formatted + " bytes";
        case 1:
            return formatted + " kb";
        case 2:
            return formatted + " Mb";
        case 3:
            return formatted + " Gb";
        default:
            return "WTF?";
        }
    }
}

Related

  1. createRealFormat()
  2. expFormatRupiah(Number input, boolean prependRp)
  3. format(final long l)
  4. format(final long value, final long divider, final String unit)
  5. format(Integer num)
  6. format(long value)
  7. format(Number value)
  8. format(Number value)
  9. format(Object num, int dev, String valueIfZero)