PDA

View Full Version : Euro or other currency value to format



luisoswaldo
31 Aug 2011, 8:28 AM
Such thing as util.format.usMoney to format the currency the euro or other currency. I was trying to duplicate the function to suit my needs, but I could not do it.

This is the code of the function




usMoney : function(v) {
v = (Math.round((v-0)*100))/100;
v = (v == Math.floor(v)) ? v + ".00" : ((v*10 == Math.floor(v*10)) ? v + "0" : v);
v = String(v);
var ps = v.split('.'),
whole = ps[0],
sub = ps[1] ? '.'+ ps[1] : '.00',
r = /(\d+)(\d{3})/;
while (r.test(whole)) { whole = whole.replace(r, '$1' + ',' + '$2');}
v = whole + sub;
if (v.charAt(0) == '-') {
return '-$' + v.substr(1);
}
return "$" + v; },

I appreciate any help i can offer.

sword-it
9 Aug 2012, 3:23 AM
Hi,

I didnt understand what you need.. Do you want override Ext.util.Format.usMoney function ? And what are your needs ?

luisoswaldo
9 Aug 2012, 4:57 AM
Yes, i need override usMoney or add others currencys in the same function.

sword-it
9 Aug 2012, 11:31 PM
Hi,
you can use Ext.override. Please see below code:

Ext.override(Ext.util.Format, {
usMoney : function(v, sign, end) {
v = (Math.round((v-0)*100))/100;
v = (v == Math.floor(v)) ? v + ".00" : ((v*10 == Math.floor(v*10)) ? v + "0" : v);
v = String(v);
var ps = v.split('.'),
whole = ps[0],
sub = ps[1] ? '.'+ ps[1] : '.00',
r = /(\d+)(\d{3})/;
while (r.test(whole)) {
whole = whole.replace(r, '$1' + ',' + '$2');
}
v = whole + sub;
if (v.charAt(0) == '-') {
return '-$' + v.substr(1);
}
if(end) {
return v + sign;
}
else {
return sign + v;
}
}
})


Example:

Ext.util.Format.usMoney('12','TL',true)
//returns "12.00TL"

luisoswaldo
3 Sep 2012, 7:47 AM
I could not do that overrides the method usMoney. Where should I place that code?

The place where I have a js function, then the onReady and then within InitComponent and not work.