Java Size bytesToString(long sizeInBytes)

Here you can find the source of bytesToString(long sizeInBytes)

Description

bytes To String

License

Open Source License

Declaration

public static String bytesToString(long sizeInBytes) 

Method Source Code

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

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

public class Main {
    public static String bytesToString(long sizeInBytes) {
        final double SPACE_KB = 1024;
        final double SPACE_MB = 1024 * SPACE_KB;
        final double SPACE_GB = 1024 * SPACE_MB;
        final double SPACE_TB = 1024 * SPACE_GB;

        NumberFormat nf = new DecimalFormat();
        nf.setMaximumFractionDigits(2);/*from w ww  . ja v  a2s  . c  o  m*/
        try {
            if (sizeInBytes < SPACE_KB) {
                return nf.format(sizeInBytes) + " Byte(s) (" + sizeInBytes
                        + ")";
            } else if (sizeInBytes < SPACE_MB) {
                return nf.format(sizeInBytes / SPACE_KB) + " KB ("
                        + sizeInBytes + ")";
            } else if (sizeInBytes < SPACE_GB) {
                return nf.format(sizeInBytes / SPACE_MB) + " MB ("
                        + sizeInBytes + ")";
            } else if (sizeInBytes < SPACE_TB) {
                return nf.format(sizeInBytes / SPACE_GB) + " GB ("
                        + sizeInBytes + ")";
            } else {
                return nf.format(sizeInBytes / SPACE_TB) + " TB ("
                        + sizeInBytes + ")";
            }
        } catch (Exception e) {
            return sizeInBytes + " Byte(s)";
        }
    }
}

Related

  1. byteCountToDisplaySize(long size)
  2. byteCountToDisplaySize(long size)
  3. byteSizeString(long bytes)
  4. BytesToHRSize(String len)
  5. bytesToSizeFormat(long bytes)
  6. bytesToUnits(long size)
  7. calcSize(double s, String type, int div)
  8. createSizeFormat(Locale locale)
  9. getBytesSpecificationDescription(double sizeInBytes, String formatSpec)