mirror of
https://github.com/OpenNebula/one.git
synced 2026-11-20 11:52:37 +09:00
feature #3748: Add humanize util
This commit is contained in:
1 parent
d065501414
commit
88d33553ee
1 file changed
+55
@@ -0,0 +1,55 @@
|
||||
define(function(require) {
|
||||
/*
|
||||
Returns a human readable size in Kilo, Mega, Giga or Tera bytes
|
||||
if no from_bytes, assumes value comes in Ks
|
||||
*/
|
||||
var _size = function(value, from_bytes, sufix) {
|
||||
if (typeof(value) === "undefined") {
|
||||
value = 0;
|
||||
}
|
||||
var binarySufix = ["", "K", "M", "G", "T"];
|
||||
|
||||
var i = from_bytes ? 0 : 1;
|
||||
while (value >= 1024 && i < 4) {
|
||||
value = value / 1024;
|
||||
i++;
|
||||
}
|
||||
value = Math.round(value * 10) / 10;
|
||||
|
||||
if (value - Math.round(value) == 0) {
|
||||
value = Math.round(value);
|
||||
}
|
||||
|
||||
if (sufix == undefined) {
|
||||
sufix = "B";
|
||||
}
|
||||
|
||||
var st = value + binarySufix[i] + sufix;
|
||||
return st;
|
||||
}
|
||||
|
||||
var _sizeFromMB = function(value) {
|
||||
if (typeof(value) === "undefined") {
|
||||
value = 0;
|
||||
}
|
||||
var binarySufix = ["MB", "GB", "TB"];
|
||||
var i = 0;
|
||||
while (value >= 1024 && i < 2) {
|
||||
value = value / 1024;
|
||||
i++;
|
||||
}
|
||||
value = Math.round(value * 10) / 10;
|
||||
|
||||
if (value - Math.round(value) == 0) {
|
||||
value = Math.round(value);
|
||||
}
|
||||
|
||||
var st = value + binarySufix[i];
|
||||
return st;
|
||||
}
|
||||
|
||||
return {
|
||||
'size': _size,
|
||||
'sizeFromMB': _sizeFromMB
|
||||
}
|
||||
})
|
||||
Reference in new issue
Block a user