skos
skos

Reputation: 4222

Currency Formatting In Sencha

I just started working on Sencha couple of hours ago. What I want is to format currency values on my app.

For example, wherever something appears like 20000 I want it to look like 20,000

I tried looking up on internet and came to know about Ext.util.Format.number

So I tried to use it like Ext.util.Format.number(total_value, “0,000.00”); wherever I was using ${total_value}. But that didn't work.

Do I have to include any external files or am I missing anything?

Upvotes: 1

Views: 4679

Answers (3)

RED.Skull
RED.Skull

Reputation: 1746

  currencyConvertion : function (value){                                               
   return Number(value).toFixed(0).replace(/./g, function(c, i, a) {
              return i > 0 && c !== "." && (a.length - i) % 3 === 0 ? "," + c : c;
                                  });
                              }

Upvotes: 0

ThinkFloyd
ThinkFloyd

Reputation: 5021

This is what I used in my project after realising Ext.util.Format.number() is not part of Sencha Touch, a small JS function which can be used anywhere in my app:

/**
 * Given a number this will format it to have comma separated readable number(Rounded off)
 * with currency symbol(Rs.) prefix
 * @example
 * Helper.formatCurrency(123456.78) = "Rs. 123,456"
 * 
 * @param {Number} num
 * @return {Number}
 */
formatCurrency : function(num) {
    num = num.toString().replace(/\$|\,/g, '');
    if (isNaN(num))
        num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    num = Math.floor(num / 100).toString();
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
        num = num.substring(0, num.length - (4 * i + 3)) + ','
                + num.substring(num.length - (4 * i + 3));
    return (((sign) ? '' : '-') + 'Rs. ' + num /*+ '.' + cents*/);
},

Feel free to copy, change and improvise

Upvotes: 2

cclerv
cclerv

Reputation: 2969

Try using Ext.util.Format.currency(“0,000.00”);

Upvotes: 0

Related Questions