Here you can find the source of BytesToHRSize(String len)
public static String BytesToHRSize(String len)
//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"; } }