Home
Analytics
Using java.text.NumberFormat in BIRT Expression Builder
rdowd
Hi All,
I'm using BIRT v2.2.1 and want to use the java.text.NumberFormat class within Expression Builder (i.e. in JavaScript code). The reason being that I want my report to display the value for this field correctly depending on the user's locale,
E.g If the value was float value of 1234.56 then in German locale this should display as 1.234,56 (i.e. "." as the thousands separator and "," as the decimal seperator)
My code is as below but I'm getting an error saying cannot convert and Object to this type, any ideas of what I might be doing wrong?
importPackage(java.text);
var value = DATAROW["height"];
var numberValue = new Number(value);
var result = NumberFormat.getInstance().format(numberValue);
result;
Perhaps I need to change the line:
var result = NumberFormat.getInstance().format(numberValue);
... to ...
var result = NumberFormat.getInstance().format(numberValue.value);
Any help would be great?
Thanks - Ro
Find more posts tagged with
Comments
johnw
importPackage(Packages.java.text);<br />
<br />
var formater = new NumberFormat(mask);<br />
var formatedNumber = formater.getInstance().format(row["myNumber"]);<br />
<br />
That should do it. Don't forget the Packages in your package import. Alternatively you can use,<br />
<br />
var formater = new Packages.java.text.NumberFormat(mask);<br />
<br />
John<br />
<br />
<br />
<blockquote class='ipsBlockquote' data-author="'rdowd'" data-cid="65746" data-time="1276946502" data-date="19 June 2010 - 04:21 AM"><p>
Hi All,<br />
<br />
I'm using BIRT v2.2.1 and want to use the java.text.NumberFormat class within Expression Builder (i.e. in JavaScript code). The reason being that I want my report to display the value for this field correctly depending on the user's locale, <br />
E.g If the value was float value of 1234.56 then in German locale this should display as 1.234,56 (i.e. "." as the thousands separator and "," as the decimal seperator)<br />
<br />
My code is as below but I'm getting an error saying cannot convert and Object to this type, any ideas of what I might be doing wrong?<br />
<br />
importPackage(java.text);<br />
<br />
var value = DATAROW["height"];<br />
var numberValue = new Number(value);<br />
var result = NumberFormat.getInstance().format(numberValue);<br />
<br />
result;<br />
<br />
Perhaps I need to change the line: <br />
var result = NumberFormat.getInstance().format(numberValue);<br />
... to ...<br />
var result = NumberFormat.getInstance().format(numberValue.value);<br />
<br />
Any help would be great?<br />
Thanks - Ro<br /></p></blockquote>
bmeyns
As NumberFormat is an abstract class, you can't instantiate a "NumberFormat" directly.<br />
But you can use its implementing subclass "DecimalFormat":<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>var formatter = new Packages.java.text.DecimalFormat("0.00");</pre>
instead of:<blockquote class='ipsBlockquote' data-author="'johnw'" data-cid="65748" data-time="1277061995" data-date="20 June 2010 - 12:26 PM"><p>
var formater = new Packages.java.text.<del class='bbc'>NumberFormat</del>(mask);<br /></p></blockquote>