Alien-Web-ExtJS-V3

 view release on metacpan or  search on metacpan

share/docs/source/Format.html  view on Meta::CPAN


<span id='Ext-util-Format-method-round'>        /**
</span>         * Rounds the passed number to the required decimal precision.
         * @param {Number/String} value The numeric value to round.
         * @param {Number} precision The number of decimal places to which to round the first parameter's value.
         * @return {Number} The rounded value.
         */
        round : function(value, precision) {
            var result = Number(value);
            if (typeof precision == 'number') {
                precision = Math.pow(10, precision);
                result = Math.round(value * precision) / precision;
            }
            return result;
        },

<span id='Ext-util-Format-method-number'>        /**
</span>         * Formats the number according to the format string.
         * &lt;div style=&quot;margin-left:40px&quot;&gt;examples (123456.789):
         * &lt;div style=&quot;margin-left:10px&quot;&gt;
         * 0 - (123456) show only digits, no precision&lt;br&gt;
         * 0.00 - (123456.78) show only digits, 2 precision&lt;br&gt;
         * 0.0000 - (123456.7890) show only digits, 4 precision&lt;br&gt;
         * 0,000 - (123,456) show comma and digits, no precision&lt;br&gt;
         * 0,000.00 - (123,456.78) show comma and digits, 2 precision&lt;br&gt;
         * 0,0.00 - (123,456.78) shortcut method, show comma and digits, 2 precision&lt;br&gt;
         * To reverse the grouping (,) and decimal (.) for international numbers, add /i to the end.
         * For example: 0.000,00/i
         * &lt;/div&gt;&lt;/div&gt;
         * @param {Number} v The number to format.
         * @param {String} format The way you would like to format this text.
         * @return {String} The formatted number.
         */
        number: function(v, format) {
            if (!format) {
                return v;
            }
            v = Ext.num(v, NaN);
            if (isNaN(v)) {
                return '';
            }
            var comma = ',',
                dec   = '.',
                i18n  = false,
                neg   = v &lt; 0;

            v = Math.abs(v);
            if (format.substr(format.length - 2) == '/i') {
                format = format.substr(0, format.length - 2);
                i18n   = true;
                comma  = '.';
                dec    = ',';
            }

            var hasComma = format.indexOf(comma) != -1,
                psplit   = (i18n ? format.replace(/[^\d\,]/g, '') : format.replace(/[^\d\.]/g, '')).split(dec);

            if (1 &lt; psplit.length) {
                v = v.toFixed(psplit[1].length);
            } else if(2 &lt; psplit.length) {
                throw ('NumberFormatException: invalid format, formats should have no more than 1 period: ' + format);
            } else {
                v = v.toFixed(0);
            }

            var fnum = v.toString();

            psplit = fnum.split('.');

            if (hasComma) {
                var cnum = psplit[0], 
                    parr = [], 
                    j    = cnum.length, 
                    m    = Math.floor(j / 3),
                    n    = cnum.length % 3 || 3,
                    i;

                for (i = 0; i &lt; j; i += n) {
                    if (i != 0) {
                        n = 3;
                    }
                    
                    parr[parr.length] = cnum.substr(i, n);
                    m -= 1;
                }
                fnum = parr.join(comma);
                if (psplit[1]) {
                    fnum += dec + psplit[1];
                }
            } else {
                if (psplit[1]) {
                    fnum = psplit[0] + dec + psplit[1];
                }
            }

            return (neg ? '-' : '') + format.replace(/[\d,?\.?]+/, fnum);
        },

<span id='Ext-util-Format-method-numberRenderer'>        /**
</span>         * Returns a number rendering function that can be reused to apply a number format multiple times efficiently
         * @param {String} format Any valid number format string for {@link #number}
         * @return {Function} The number formatting function
         */
        numberRenderer : function(format) {
            return function(v) {
                return Ext.util.Format.number(v, format);
            };
        },

<span id='Ext-util-Format-method-plural'>        /**
</span>         * Selectively do a plural form of a word based on a numeric value. For example, in a template,
         * {commentCount:plural(&quot;Comment&quot;)}  would result in &quot;1 Comment&quot; if commentCount was 1 or would be &quot;x Comments&quot;
         * if the value is 0 or greater than 1.
         * @param {Number} value The value to compare against
         * @param {String} singular The singular form of the word
         * @param {String} plural (optional) The plural form of the word (defaults to the singular with an &quot;s&quot;)
         */
        plural : function(v, s, p) {
            return v +' ' + (v == 1 ? s : (p ? p : s+'s'));
        },



( run in 0.590 second using v1.01-cache-2.11-cpan-adec679a428 )