[pve-devel] [PATCH manager 2/5] generalize format_size function

Dominik Csapak d.csapak at proxmox.com
Fri Apr 15 15:26:26 CEST 2016


now it calculates the size in a general way,
up to the size defined in an array

Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
 www/manager6/Utils.js | 28 +++++-----------------------
 1 file changed, 5 insertions(+), 23 deletions(-)

diff --git a/www/manager6/Utils.js b/www/manager6/Utils.js
index 1437449..2c536aa 100644
--- a/www/manager6/Utils.js
+++ b/www/manager6/Utils.js
@@ -622,32 +622,14 @@ Ext.define('PVE.Utils', { statics: {
     format_size: function(size) {
 	/*jslint confusion: true */
 
-	if (size < 1024) {
-	    return size;
-	}
-
-	var kb = size / 1024;
-
-	if (kb < 1024) {
-	    return kb.toFixed(0) + "KiB";
-	}
-
-	var mb = size / (1024*1024);
+	var units = ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi', 'Yi'];
+	var num = 0;
 
-	if (mb < 1024) {
-	    return mb.toFixed(0) + "MiB";
+	while (size >= 1024 && ((num++)+1) < units.length) {
+	    size = size / 1024;
 	}
 
-	var gb = mb / 1024;
-
-	if (gb < 1024) {
-	    return gb.toFixed(2) + "GiB";
-	}
-
-	var tb =  gb / 1024;
-
-	return tb.toFixed(2) + "TiB";
-
+	return size.toFixed((num > 0)?2:0) + " " + units[num] + "B";
     },
 
     format_html_bar: function(per, text) {
-- 
2.1.4





More information about the pve-devel mailing list