Added by thebman220 on July-3-2008, 12:38 am
/**
* Get the human-readable size for an amount of bytes
* @param int size : the number of bytes to be converted
* @param int precision : number of decimal places to round to;
* optional - defaults to 2
* @param boolean long_name : whether or not the returned size tag
* should be unabbreviated (ie "Gigabytes"
* or "GB"); optional - defaults to true
* @param boolean real_size : whether or not to use the real (base
* 1024) or commercial (base 1000) size;
* optional - defaults to true
* @return String : the converted size
*/
static public String getSize(int size) {
return getSize(size,2,true,true);
}
static public String getSize(int size,int precision) {
return getSize(size,precision,true,true);
}
static public String getSize(int size,int precision,boolean longName) {
return getSize(size,2,longName,true);
}
static public String getSize(int size,int precision,
boolean longName,boolean realSize) {
int base=realSize?1024:1000;
int pos=0;
double decSize=(double)size;
while (decSize>base) {
decSize/=base;
pos++;
}
String prefix=getSizePrefix(pos);
String sizeName=longName?prefix+"bytes":""+prefix.charAt(0)+"B";
sizeName=sizeName.substring(0,1).toUpperCase()+sizeName.substring(1);
int num=(int)Math.pow(10,precision);
return (Math.round(decSize*num)/num)+" "+sizeName;
}
/**
* @param int pos : the distence along the metric scale relitive to 0
* @return string : the prefix
*/
static public String getSizePrefix(int pos) {
switch (pos) {
case 0: return "";
case 1: return "kilo";
case 2: return "mega";
case 3: return "giga";
case 4: return "tera";
case 5: return "peta";
case 6: return "exa";
case 7: return "zetta";
case 8: return "yotta";
case 9: return "xenna";
case 10: return "w-";
case 11: return "vendeka";
case 12: return "u-";
default: return "?-";
}
}
Added by thebman220 on July-9-2008, 11:31 pm
Added by thebman220 on July-5-2008, 12:50 am
Added by thebman220 on July-5-2008, 12:03 am
Added by thebman220 on July-4-2008, 11:12 pm
Added by thebman220 on July-4-2008, 11:02 pm