tidy yp formating
[roojs1] / Roo / util / Format.js
index 22bb668..249fd70 100644 (file)
@@ -171,12 +171,15 @@ Roo.util.Format = function(){
          * eventually this should probably emulate php's number_format
          * @param {Number/String} value The numeric value to format
          * @param {Number} decimals number of decimal places
+         * @param {String} delimiter for thousands (default comma)
          * @return {String} The formatted currency string
          */
-        number : function(v,decimals)
+        number : function(v, decimals, thousandsDelimiter)
         {
             // multiply and round.
             decimals = typeof(decimals) == 'undefined' ? 2 : decimals;
+            thousandsDelimiter = typeof(thousandsDelimiter) == 'undefined' ? ',' : thousandsDelimiter;
+            
             var mul = Math.pow(10, decimals);
             var zero = String(mul).substring(1);
             v = (Math.round((v-0)*mul))/mul;
@@ -188,17 +191,16 @@ Roo.util.Format = function(){
             var ps = v.split('.');
             var whole = ps[0];
             
-            
             var r = /(\d+)(\d{3})/;
             // add comma's
-            while (r.test(whole)) {
-                whole = whole.replace(r, '$1' + ',' + '$2');
-            }
             
+            if(thousandsDelimiter.length != 0) {
+                whole = whole.replace(/\B(?=(\d{3})+(?!\d))/g, thousandsDelimiter );
+            } 
             
             var sub = ps[1] ?
                     // has decimals..
-                    (decimals ?  ('.'+ ps[1] + zero.substring(ps[1].length) : '') :
+                    (decimals ?  ('.'+ ps[1] + zero.substring(ps[1].length)) : '') :
                     // does not have decimals
                     (decimals ? ('.' + zero) : '');
             
@@ -219,7 +221,7 @@ Roo.util.Format = function(){
             if(!(v instanceof Date)){
                 v = new Date(Date.parse(v));
             }
-            return v.dateFormat(format || "m/d/Y");
+            return v.dateFormat(format || Roo.util.Format.defaults.date);
         },
 
         /**
@@ -243,6 +245,28 @@ Roo.util.Format = function(){
          */
         stripTags : function(v){
             return !v ? v : String(v).replace(this.stripTagsRE, "");
+        },
+        
+        /**
+         * Size in Mb,Gb etc.
+         * @param {Number} value The number to be formated
+         * @param {number} decimals how many decimal places
+         * @return {String} the formated string
+         */
+        size : function(value, decimals)
+        {
+            var sizes = ['b', 'k', 'm', 'g', 't'];
+            if (value == 0) {
+                return 0;
+            }
+            var i = parseInt(Math.floor(Math.log(value) / Math.log(1024)));
+            return Roo.util.Format.number(value/ Math.pow(1024, i) ,decimals)   + sizes[i];
         }
+        
+        
+        
     };
-}();
\ No newline at end of file
+}();
+Roo.util.Format.defaults = {
+    date : 'd/M/Y'
+};
\ No newline at end of file