Java Size BytesToHRSize(String len)

Here you can find the source of BytesToHRSize(String len)

Description

Bytes To HR Size

License

Open Source License

Declaration

public static String BytesToHRSize(String len) 

Method Source Code

//package com.java2s;
//   Licensed under the Apache License, Version 2.0 (the "License"); you may

import java.text.DecimalFormat;

public class Main {
    public static String BytesToHRSize(String len) {
        if (len.length() < 5) {
            if (Integer.parseInt(len) <= 1024) {
                return len + " bytes";
            }// w ww  . ja v a2s  .  co m
        }
        //if bigger than 1k, divide by 1024 and slap a type on the end
        DecimalFormat format = new DecimalFormat("0.000");
        double newlen = Double.parseDouble(len);
        newlen = newlen / 1024.0;
        if (newlen <= 1024.0) {
            return format.format(newlen) + " K";
        }

        newlen = newlen / 1024.0;
        if (newlen <= 1024.0) {
            return format.format(newlen) + " M";
        }

        newlen = newlen / 1024.0;
        if (newlen <= 1024.0) {
            return format.format(newlen) + " G";
        }

        newlen = newlen / 1024.0;
        if (newlen <= 1024.0) {
            return format.format(newlen) + " T";
        }

        newlen = newlen / 1024.0;
        if (newlen <= 1024.0) {
            return format.format(newlen) + " P";
        }
        return format.format(newlen) + " E";
    }
}

Related

  1. byteCountToDisplaySize(long bytes)
  2. byteCountToDisplaySize(long size)
  3. byteCountToDisplaySize(long size)
  4. byteCountToDisplaySize(long size)
  5. byteSizeString(long bytes)
  6. bytesToSizeFormat(long bytes)
  7. bytesToString(long sizeInBytes)
  8. bytesToUnits(long size)
  9. calcSize(double s, String type, int div)