grid: Formatting numbers

Hi,

is it possible to change the format of numbers for a grid column?
In Germany we show -> 1.234,56 instead of 1,234.56.
At the moment I use a function in template, but it would be easier, using a global property.

Thanky you
Robert

We have it in our roadmap, but for now, formatting through template, is the only possible way.

May you please post a sample showing how to format a number through template?

Code excerpt

{ 
view:"grid",
id:"griddata",
fields:[{
	id:"cat",
	label:"Category",
	template:"#category#",
	width:150
},
{
	id:"tot",
	width:300,
	label:"Total",
	template:"#total#"
}],
datatype:"json",
url:"./data/data.php"
},

I need to format the “total” field this way: 1234.56 => 1.234,56

Hi,

for me I solved it by calling a function to format the number:

template: function( obj ) { return formatNumber( obj, "num" ); }

Hope this helps
Robert

Well, it is a begining but I really need something more detailed. In your code what does obj and “num” stands for? Is this line added to view:grid or fields collection? I’m not getting which property you’re using to get the field value inside your function.
May you post a more complete piece of code? I’m sure it can work as a sample for many forum users that don’t live in the US and need to format numbers in a different way.
Thanks in advance.

Well, ok. :wink:

So this is the function for formatting the number:

function formatNumber( obj, fieldName ) { oNum.setNumber( obj[ fieldName ] ); oNum.setPlaces(0); return oNum.toFormatted(); }

“oNum” is an instance of a number formatting library.
“num” in the above example is the fieldname of the record. “obj” is the item to show, which will be the argument of the function in “template”.

Hope this helps.

Best regards
Robert

Yes, this helped a lot! :smiley:

I made a few modifications to your function and now I can have numbers formatted the way I need to.

Modified code

function formatNumber( obj, fieldName ) {
   var oNum = new NumberFormat(obj[ fieldName ]);
   oNum.setNumber( obj[ fieldName ] );
   oNum.setPlaces(2);
   oNum.setSeparators(true, ".", ",");
   return oNum.toFormatted();
}

Thank you very much!

You are welcome. :wink:

Just one hint:
Don’t instantiate NumberFormat every time the function will be called. It takes too much time.
It’s more faster if you instantiate it one time (outside) and reuse this instance in the function.

Best regards
Robert