PDA

View Full Version : Other currency formats other than usMoney ?



DrZog
30 Apr 2007, 5:38 AM
Are there plans to support other currency formats (e.g.

brian.moeskau
1 May 2007, 2:43 AM
There are no plans for including additional formats directly in Ext as the complete list would be quite long and most people would never need most of them. You can easily provide your own by adding your own function anywhere after Ext.util.Format is included:

[CODE]
Ext.util.Format.gbMoney = 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);
return "

DrZog
1 May 2007, 2:47 AM
Cheers Brian. Your way is much slicker than a custom renderer, I'm sure many will find this fragment useful.

willydee
1 May 2007, 8:47 AM
Almost the same, just replacing the decimal separator:

[code]
// plug German currency renderer into formatter
Ext.util.Format.deMoney = 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);
return (v + '

jsakalos
2 May 2007, 3:33 AM
Thanks Brian.

I also need this.

violinista
29 Aug 2007, 8:14 AM
After a while, in need for some more advanced currency formatting with thousand separating, I found complete solution there (http://jsfromhell.com//number/fmt-money) and adopted it to use with extJs; I use it heavily in my datagrids, as a renderer. The customizable params are intentionally put in function body, for using it in renderer,without touch.

Here it is:

[php]
/**
* Improved currency formatter with thousands separator
* Configurable options:
* c - number of digits after decimal separator
* d - decimal separator
* t - thousand separator
* s - currency symbol
* @param {Object} number/string which have to be formatted!
* @return {string} formatted number
*/
Ext.util.Format.Currency = function(n) {
var c=2, d=",", t=".", s="

mystix
29 Aug 2007, 8:20 AM
just for fun (don't hit me ;)), why not create a factory
[code][s]
Ext.util.Format.CurrencyFactory = function(c, d, t, s) {
return function(n) {
var m = (c = Math.abs(c) + 1 ? c : 2, d = d || ",", t = t || ".",
/(d+)(?:(.d+)|)/.exec(n + "")), x = m[1].length > 3 ? m[1].length % 3 : 0;
return ((x ? m[1].substr(0, x) + t : "") + m[1].substr(x).replace(/(d{3})(?=d)/g,
"$1" + t) + (c ? d + (+m[2] || 0).toFixed(c).substr(2) : ""))+" "+s;
}
}

var euroFormatter = Ext.util.Format.CurrencyFactory(2, ",", ".", "€");
var dollarFormatter = Ext.util.Format.CurrencyFactory(2, ",", ".", "$");
var yenFormatter = Ext.util.Format.CurrencyFactory(2, ",", ".", "

violinista
29 Aug 2007, 8:27 AM
Thanks! Much useful, I am heavily using factory pattern in my backend code, but forgot about it in Js... But improving every day! ;)

HINT: "$" and some other symbols must reside BEFORE currency number, thing 'bout it! Maybe some switch ... case statement before value returnig, to determine whether symbol resides before or after currency amount.

10X!

[EDIT 1]: Not sure how to resolve your bugs really. Xaxaxa

ronnyr
15 Sep 2007, 7:21 AM
I a reply to willydee's code I wrote a little change.
As it hapens the € sign does not work in some occasions and I replaced it with €



Willydee's code :


// plug German currency renderer into formatter
Ext.util.Format.deMoney = 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);
return (v + ' €').replace(/\./, ',');
};


My changed code:


// plug European currency renderer into formatter
Ext.util.Format.Currency = 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);
return ('€' + v).replace(/\./, ',');
};

trbs
16 Sep 2007, 4:52 AM
Having Euro (and Yen and other major currencies) in the Ext distro is a must, IMHO.
I'm +1 so it doesn't feel so American only ;)

kayegil.hauan
4 Oct 2007, 11:50 AM
Having Euro (and Yen and other major currencies) in the Ext distro is a must, IMHO.
I'm +1 so it doesn't feel so American only ;)

I totally agree. But also should currency translations come as a part of the locale translation file. Just as important as having dates translated to a regional format is having the currency format translated.

I guess Violinistas function would cover most currency formats.. It lets you choose your own currency notation, thousand separator and decimal separator. Maybe should you even be able to choose wether the currency notation should come before or after the value...?

devil1591
21 Nov 2007, 5:31 AM
Here's my 2 cents for French formatting, you can include this in the locale file ext-lang-fr.js


if (Ext.util.Format) {
Ext.util.Format.frMoney = 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('.');
var whole = ps[0];
var sub = ps[1] ? ','+ ps[1] : ',00';
var 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 + " €";
};
}

cabster
25 Feb 2008, 5:54 AM
[QUOTE=brian.moeskau;27131]There are no plans for including additional formats directly in Ext as the complete list would be quite long and most people would never need most of them. You can easily provide your own by adding your own function anywhere after Ext.util.Format is included:

[CODE]
Ext.util.Format.gbMoney = 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);
return "

siyuan
31 Aug 2008, 8:08 PM
[QUOTE=mystix;58646]just for fun (don't hit me ;)), why not create a factory
[code][s]
Ext.util.Format.CurrencyFactory = function(c, d, t, s) {
return function(n) {
var m = (c = Math.abs(c) + 1 ? c : 2, d = d || ",", t = t || ".",
/(d+)(?:(.d+)|)/.exec(n + "")), x = m[1].length > 3 ? m[1].length % 3 : 0;
return ((x ? m[1].substr(0, x) + t : "") + m[1].substr(x).replace(/(d{3})(?=d)/g,
"$1" + t) + (c ? d + (+m[2] || 0).toFixed(c).substr(2) : ""))+" "+s;
}
}

var euroFormatter = Ext.util.Format.CurrencyFactory(2, ",", ".", "

mystix
31 Aug 2008, 8:49 PM
[QUOTE=siyuan;216991][code]
Ext.util.Format.CurrencyFactory = function(c, d, t, s) {
return function(n) {
var m = (c = Math.abs(c) + 1 ? c : 2, d = d || ",", t = t || ".",
/(\d+)(?:(\.\d+)|)/.exec(n + "")), x = m[1].length > 3 ? m[1].length % 3 : 0;
return ((x ? m[1].substr(0, x) + t : "") + m[1].substr(x).replace(/(d{3})(?=d)/g,
"$1" + t) + (c ? d + (+m[2] || 0).toFixed(c).substr(2) : ""))+" "+s;
}
}

var euroFormatter = Ext.util.Format.CurrencyFactory(2, ",", ".", "

siyuan
31 Aug 2008, 9:54 PM
[QUOTE=siyuan;216991][code]
Ext.util.Format.CurrencyFactory = function(c, d, t, s) {
return function(n) {
var m = (c = Math.abs(c) + 1 ? c : 2, d = d || ",", t = t || ".",
/(\d+)(?:(\.\d+)|)/.exec(n + "")), x = m[1].length > 3 ? m[1].length % 3 : 0;
return ((x ? m[1].substr(0, x) + t : "") + m[1].substr(x).replace(/(d{3})(?=d)/g,
"$1" + t) + (c ? d + (+m[2] || 0).toFixed(c).substr(2) : ""))+" "+s;
}
}

var euroFormatter = Ext.util.Format.CurrencyFactory(2, ",", ".", "

fxmisticat
12 Jan 2009, 7:52 PM
Ext.util.Format.CurrencyFactory = function(c, d, t, s) {
return function(n) {
var m = (c = Math.abs(c) + 1 ? c : 2, d = d || ",", t = t || ".",
/(\d+)(?:(\.\d+)|)/.exec(n + "")), x = m[1].length > 3 ? m[1].length % 3 : 0;
return ((x ? m[1].substr(0, x) + t : "") + m[1].substr(x).replace(/(\d{3})(?=\d)/g,
"$1" + t) + (c ? d + (+m[2] || 0).toFixed(c).substr(2) : ""))+" "+s;
}
}

Works great.. except if it's a negative number it drops the - sign

mystix
15 Jan 2009, 6:01 PM
the decimal and thousand separators are reversed
to keep the minus sign, simply store it somewhere if it exists
the parameter names are confusing



Ext.util.Format.CurrencyFactory = function(dp, dSeparator, tSeparator, symbol) {
return function(n) {
dp = Math.abs(dp) + 1 ? dp : 2;
dSeparator = dSeparator || ".";
tSeparator = tSeparator || ",";

var m = /(\d+)(?:(\.\d+)|)/.exec(n + ""),
x = m[1].length > 3 ? m[1].length % 3 : 0;

return (n < 0? '-' : '') // preserve minus sign
+ (x ? m[1].substr(0, x) + tSeparator : "")
+ m[1].substr(x).replace(/(\d{3})(?=\d)/g, "$1" + tSeparator)
+ (dp? dSeparator + (+m[2] || 0).toFixed(dp).substr(2) : "")
+ " " + symbol;
};
};


you might also want to check out @condor's number formatter at
http://extjs.com/forum/showthread.php?t=48600

fxmisticat
15 Jan 2009, 6:14 PM
very nice thank you!

mystix
15 Jan 2009, 6:32 PM
sorry, forgot to fix the reversed decimal / thousand separators in the code.
also fixed some spelling errors (yes i'm a perfectionist...)
check the previous post again.

fxmisticat
15 Jan 2009, 6:56 PM
Got it! Thanks again mystix!

Arno.Nyhm
7 Apr 2009, 6:45 AM
thx to mystix :-)

here are my changes:

- remove space between number and symbol so you can decide by yourself with the symbol parameter if you like to have a space
- add parameter for position of symbol




if (Ext.util.Format) {
if (!Ext.util.Format.CurrencyFactory) {
Ext.util.Format.CurrencyFactory = function(dp, dSeparator, tSeparator, symbol, rightPosition) {
return function(n) {

dp = Math.abs(dp) + 1 ? dp : 2;
dSeparator = dSeparator || ".";
tSeparator = tSeparator || ",";
symbol = symbol || "$";
rightPosition = rightPosition || false;

var m = /(\d+)(?:(\.\d+)|)/.exec(n + ""),
x = m[1].length > 3 ? m[1].length % 3 : 0;

var v = (n < 0? '-' : '') // preserve minus sign
+ (x ? m[1].substr(0, x) + tSeparator : "")
+ m[1].substr(x).replace(/(\d{3})(?=\d)/g, "$1" + tSeparator)
+ (dp? dSeparator + (+m[2] || 0).toFixed(dp).substr(2) : "");

return rightPosition?v+symbol:symbol+v;
};
};
}
}
usage:


var eurFormatter = Ext.util.Format.CurrencyFactory(2, ",", ".", "\u20ac", true);
var value = 12345.67;
return eurFormatter(value); //--> 12.345,67

var usFormatter = Ext.util.Format.CurrencyFactory();
var value = 12345.67;
return usFormatter(value); //--> $12,345.67

Arno.Nyhm
7 Apr 2009, 6:52 AM
if you dont like to give the space into the symbol parameter then use this:



if (Ext.util.Format) {
if (!Ext.util.Format.CurrencyFactory) {
Ext.Ext.util.Format.CurrencyFactory = function(hasSpace, dp, dSeparator, tSeparator, symbol, rightPosition) {
return function(n) {

var spaceText = hasSpace?" ":"";
dp = Math.abs(dp) + 1 ? dp : 2;
dSeparator = dSeparator || ".";
tSeparator = tSeparator || ",";
symbol = symbol || "$";
rightPosition = rightPosition || false;

var m = /(\d+)(?:(\.\d+)|)/.exec(n + ""),
x = m[1].length > 3 ? m[1].length % 3 : 0;

var v = (n < 0? '-' : '') // preserve minus sign
+ (x ? m[1].substr(0, x) + tSeparator : "")
+ m[1].substr(x).replace(/(\d{3})(?=\d)/g, "$1" + tSeparator)
+ (dp? dSeparator + (+m[2] || 0).toFixed(dp).substr(2) : "");

return rightPosition?v+spaceText+symbol:symbol+spaceText+v;
};
};
}
}
usage:



var eurFormatter = Ext.util.Format.CurrencyFactory(false,2, ",", ".", "\u20ac", true);
var value = 12345.67;
return eurFormatter(value); //--> 12.345,67

var eurFormatter = Ext.util.Format.CurrencyFactory(true,2, ",", ".", "\u20ac", true);
var value = 12345.67;
return eurFormatter(value); //--> 12.345,67

var usFormatter = Ext.util.Format.CurrencyFactory();
var value = 12345.67;
return usFormatter(value); //--> $12,345.67

var usFormatter = Ext.util.Format.CurrencyFactory(true);
var value = 12345.67;
return usFormatter(value); //--> $ 12,345.67

sosy
2 Aug 2010, 2:10 AM
Maybe a bit older thread, but it was usefull for me!

Pimped it a bit to use it in an EditorGrid, so that you can type in values with commata, then transform it to Euro value, or if there is no value an - to keep the grid much cleaner to look ad.



//
// EURO
//
Ext.util.Format.Currency = function(v)
{
v = Ext.num(String(v).replace(/\,/, '.'));
v = (Math.round((v-0)*100))/100;
v = (v == Math.floor(v)) ? v + ".00" : ((v*10 == Math.floor(v*10)) ? v + "0" : v);
if (v==0)
{
v = '-';
return (v);
}
else
{
return (v + ' &euro;').replace(/\./, ',');
}
};


Hope it is usefull for some of you..

Regards

Sosy

jsakalos
2 Aug 2010, 2:40 AM
Thank you for sharing.

yassyboy
6 Oct 2010, 10:52 PM
Hi All,

I am beginner in ExtJS so bear my newbie Questions.

Where do I place the custom currency render-er? In the beginning of the code? Any examples will be higher appreciated.

Eg. "replaceParameter.push({oldStr:"{{totalPaidAmount}}", newStr:Ext.util.Format.usMoney(totalPaidAmount)}); "

If I want to put "gbMoney" how do I do it .. in a very layman terms.

Thank you

Kind Regards

Yassyboy

Condor
7 Oct 2010, 3:27 AM
Ext.util.Format.gbMoney = function(value){
return ...
};

Pachat
16 Dec 2010, 1:20 AM
If you want to emphasize on usMoney, here is an example for Euro :

Euro = function(v)
{
v = Ext.util.Format.usMoney(v);
if (v=='$0.00')
return '-';
else
return v.replace(/\$/, '').replace(/\,/g, ' ').replace(/\./, ',')+' &euro';
};

citstudio
30 Oct 2011, 11:24 PM
I've made some modification from original form (usMoney) function
this is for japan currency (yen = )


Ext.util.Format.jpyMoney = function(v) { // override Ext.util.usMoney
v = Ext.num(v, 0); // ensure v is a valid numeric value, otherwise use 0 as a base (fixes $NaN.00 appearing in summaryRow when no records exist)
v = (Math.round((v - 0) * 100)) / 100;
v = (v == Math.floor(v)) ? v + "" : ((v * 10 == Math.floor(v * 10)) ? v + "0" : v);
v = String(v);
var ps = v.split('.');
var whole = ps[0];
var sub = ps[1] ? '.'+ ps[1] : '';
var 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;
}

here it's for other currency's symbols :
http://www.hypergurl.com/asciisymbols.html

tolisc
10 Sep 2012, 5:25 AM
Ext.util.Format.euroCurrency = function(v){
return Ext.util.Format.currency(v, '&euro;', 2);
};

and in your app launch
launch: function() {
if (Ext.util.Format) {
Ext.apply(Ext.util.Format, {
thousandSeparator : '.',
decimalSeparator : ','
});
};
}