Home
Analytics
Default number formatting
daniel_v
Hello,<br />
<br />
I'm using BIRT 2.5.2,<br />
I have a strange issue related to the default number formatting. <br />
The system decimal separator is not used when the report Data value is of type decimal and float.<br />
same issue when using Dynamic Text instead of Data box.<br />
<br />
Only when a Data field is used and the type is set to String, the correct decimal separator is used!<br />
<br />
My PC is set to use German format so I'm expecting to have a decimal values separated by commas instead of dots.<br />
<br />
Ex. Expected : <strong class='bbc'>0,33</strong><br />
Dynamic text: <strong class='bbc'>Math.round(1/3*100)/100</strong> => <strong class='bbc'>0.33</strong> -WRONG<br />
Data field: <strong class='bbc'>Math.round(10*5/3*100)/100</strong> Data Type: <strong class='bbc'>Decimal</strong>or <strong class='bbc'>Float</strong> => <strong class='bbc'>0.33</strong> -WRONG<br />
<br />
Data field <strong class='bbc'>Math.round(10*5/3*100)/100</strong> Data Type: <strong class='bbc'>String</strong> => <strong class='bbc'>0,33</strong> - OK<br />
<br />
<br />
<br />
Any help is appreciated.<br />
Thanks in advance
Find more posts tagged with
Comments
daniel_v
I have also tried to "force" a text/string values in the Dynamic Text field like this:<br />
<br />
<strong class='bbc'>(Math.round(1/3*100)/100).toString()</strong><br />
<br />
and<br />
<br />
<strong class='bbc'>""+(Math.round(1/3*100)/100)</strong><br />
<br />
but the problem is still the same, the output is <strong class='bbc'>0.33</strong> instead of <strong class='bbc'>0,33</strong>
mwilliams
If I set the default locale of my Eclipse to German (in the preferences) and use the same expression as you're using, I get the correct format only when using Float or Decimal. Not when using String or a Dynamic Text Box.
daniel_v
<blockquote class='ipsBlockquote' data-author="'mwilliams'" data-cid="104646" data-time="1340131020" data-date="19 June 2012 - 11:37 AM"><p>
If I set the default locale of my Eclipse to German (in the preferences) and use the same expression as you're using, I get the correct format only when using Float or Decimal. Not when using String or a Dynamic Text Box.<br /></p></blockquote>
<br />
Hi Michael,<br />
<br />
Thanks for your quick answer, I have not changed the locale in the BIRT Report Designer (2.5.2) but checking that under Preferences I have remarked that the selected local was <strong class='bbc'>dansk(Danmark)</strong>, as previously I have changed the PC local to Denmark, and the only problem is that the selected local (detected automatically by the RCP Designer) started with lowercase <strong class='bbc'>d</strong> - using this as previously mentioned only the Data field of type String was correctly formatted.<br />
Changing that to uppercase <strong class='bbc'>D</strong> - <strong class='bbc'>Dansk(Danmark)</strong> both Data fields of type String and Decimal are correctly formatted.<br />
<br />
BUG: Auto detected locals in BIRT RCP Designer 2.5.2 for Denmark system format, start with lowercase letter and the decimal number formatting is wrong, only the Data fields of type String are correctly formatted.<br />
<br />
How to reproduce : Set PC Format (Under Regions and Languages) to Danish (Denmark) then Open/Restart BIRT RCP Designer -> Window -> Preferences -> Select Report Design then Preview<br />
Empty the field "Choose your locale" then select Apply and OK, reopen the Preferences window will show the default locale starting with lowercase letter like this : <strong class='bbc'>dansk (Danmark)</strong><br />
<br />
Is there a simple way to set the correct decimal number formatting for a Dynamic Text Box or we must use a Data fields instead?<br />
<br />
Thanks in advance
mwilliams
This will work in a dynamic text to get German.<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
importPackage (Packages.java.text);
importPackage (Packages.java.util);
temp = 0.33;
nf = NumberFormat.getInstance(Locale.GERMAN);
nf.format(temp);
</pre>
daniel_v
<blockquote class='ipsBlockquote' data-author="'mwilliams'" data-cid="104660" data-time="1340143727" data-date="19 June 2012 - 03:08 PM"><p>
This will work in a dynamic text to get German.<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
importPackage (Packages.java.text);
importPackage (Packages.java.util);
temp = 0.33;
nf = NumberFormat.getInstance(Locale.GERMAN);
nf.format(temp);
</pre></p></blockquote>
<br />
Thanks a lot it works well, just one more question related to this, is it necessarily to use this code for using the default number formatting for Dynamic Text:<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
importPackage (Packages.java.text);
importPackage (Packages.java.util);
temp = 0.33;
nf = NumberFormat.getInstance();
nf.format(temp);
</pre>
<br />
or it has been fixed in the recent BIRT versions ( > 2.5.2) so the dynamic text is automatically formatted with the default locale?<br />
<br />
Thanks in advance
daniel_v
<blockquote class='ipsBlockquote' data-author="'daniel_v'" data-cid="104669" data-time="1340170737" data-date="19 June 2012 - 10:38 PM"><p>
Thanks a lot it works well, just one more question related to this, is it necessarily to use this code for using the default number formatting for Dynamic Text:<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
importPackage (Packages.java.text);
importPackage (Packages.java.util);
temp = 0.33;
nf = NumberFormat.getInstance();
nf.format(temp);
</pre>
<br />
or it has been fixed in the recent BIRT versions ( > 2.5.2) so the dynamic text is automatically formatted with the default locale?<br />
<br />
Thanks in advance<br /></p></blockquote>
<br />
<br />
HI again,<br />
<br />
In the report designer the following code works well :<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
importPackage (Packages.java.text);
importPackage (Packages.java.util);
temp = 0.33;
nf = NumberFormat.getInstance();
nf.format(temp);
</pre>
<br />
But does not work into my report viewer, I have specified the __locale parameter as __locale=da_DK<br />
<br />
It seams that the default Locale used by the report viewer is not that specified by the __locale parameter?<br />
<br />
Thanks in advance<br />
<br />
PS: I have remarked that there are lot of SPAM replies to this topic :S
mwilliams
Does it work if you put the Locale into the format? i.e. nf = NumberFormat.getInstance(new Locale("da","DK"));
daniel_v
<blockquote class='ipsBlockquote' data-author="'mwilliams'" data-cid="104937" data-time="1340200167" data-date="20 June 2012 - 06:49 AM"><p>
Does it work if you put the Locale into the format? i.e. nf = NumberFormat.getInstance(new Locale("da","DK"));<br /></p></blockquote>
<br />
Hi Michael,<br />
<br />
Thanks again for your answer, YES, setting the Locale into the format works well<br />
i.e <br />
<pre class='_prettyXprint _lang-auto _linenums:0'>i.e. nf = NumberFormat.getInstance(new Locale("da","DK"));</pre>
<br />
But this solution does not solve my problem as I need to have dynamic locale in the reports - specified in the report URL (... __locale=<user_locale>)<br />
<br />
Example:<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>http://localhost:8900/WebViewerExampel/frameset?_-report=MyReport.rptdesign&__locale=fr_CA</pre>
<br />
Into the BIRT documentation I have found the following about the _locale parameter :<br />
<blockquote class='ipsBlockquote' ><p>Specifies the locale for the specific operation.<br />
Note that this will override the default locale.<br />
Order of precedence is as follows:<br />
<br />
__locale parameter.<br />
<br />
Locale from client browser.<br />
<br />
Locale web.xml setting.<br />
<br />
Locale for the application server.</p></blockquote>
<br />
But it seams that is not working like this on my report viewer 2.5.2
<br />
<br />
Any suggestions are very appreciated!<br />
Thanks in advance
daniel_v
<blockquote class='ipsBlockquote' data-author="'mwilliams'" data-cid="104937" data-time="1340200167" data-date="20 June 2012 - 06:49 AM"><p>
Does it work if you put the Locale into the format? i.e. nf = NumberFormat.getInstance(new Locale("da","DK"));<br /></p></blockquote>
<br />
Hi Michael,<br />
<br />
Thanks again for your answer, YES, setting the Locale into the format works well<br />
i.e <br />
<pre class='_prettyXprint _lang-auto _linenums:0'>i.e. nf = NumberFormat.getInstance(new Locale("da","DK"));</pre>
<br />
But this solution does not solve my problem as I need to have dynamic locale in the reports - specified in the report URL (... __locale=<user_locale>)<br />
<br />
Example:<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>http://localhost:8900/WebViewerExampel/frameset?_-report=MyReport.rptdesign&__locale=fr_CA</pre>
<br />
Into the BIRT documentation I have found the following about the _locale parameter :<br />
<blockquote class='ipsBlockquote' ><p>Specifies the locale for the specific operation.<br />
Note that this will override the default locale.<br />
Order of precedence is as follows:<br />
<br />
__locale parameter.<br />
<br />
Locale from client browser.<br />
<br />
Locale web.xml setting.<br />
<br />
Locale for the application server.</p></blockquote>
<br />
But it seams that is not working like this on my report viewer 2.5.2
<br />
<br />
Any suggestions are very appreciated!<br />
Thanks in advance
mwilliams
Try this:<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
importPackage (Packages.java.text);
importPackage (Packages.java.util);
temp = 0.33;
localeCode = reportContext.getLocale();
lang = localeCode.getLanguage();
country = localeCode.getCountry();
nf = NumberFormat.getInstance(new Locale(lang,country));
nf.format(temp);
</pre>
daniel_v
With my latest tests I have found that when I'm using a number formatting without locale (i.e with default locale) then the JVM locale will be used as it's noted into the JAVA doc for the default locale :<br />
<br />
<blockquote class='ipsBlockquote' ><p>Gets the current value of the default locale for this instance of the Java Virtual Machine.</p></blockquote>
<br />
and that is the reason why the number formatting is wrong in my reports.<br />
<br />
All I need is to have a number formatting based on the __locale parameters specified in the report URL,<br />
and it seams that the only way is to use Data boxes with Decimal data type specified?<br />
<br />
Note: I'm using BIRT Viewer 2.5.2<br />
<br />
Any help is appreciated!<br />
Thanks in advance
daniel_v
<blockquote class='ipsBlockquote' data-author="'mwilliams'" data-cid="104964" data-time="1340257522" data-date="20 June 2012 - 10:45 PM"><p>
Try this:<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
importPackage (Packages.java.text);
importPackage (Packages.java.util);
temp = 0.33;
localeCode = reportContext.getLocale();
lang = localeCode.getLanguage();
country = localeCode.getCountry();
nf = NumberFormat.getInstance(new Locale(lang,country));
nf.format(temp);
</pre></p></blockquote>
<br />
<br />
Ahh using the report context locale instead of the system locale, I will check it and back to you shortly,<br />
Thanks
daniel_v
<blockquote class='ipsBlockquote' data-author="'mwilliams'" data-cid="104964" data-time="1340257522" data-date="20 June 2012 - 10:45 PM"><p>
Try this:<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
importPackage (Packages.java.text);
importPackage (Packages.java.util);
temp = 0.33;
localeCode = reportContext.getLocale();
lang = localeCode.getLanguage();
country = localeCode.getCountry();
nf = NumberFormat.getInstance(new Locale(lang,country));
nf.format(temp);
</pre></p></blockquote>
<br />
This solution works great (the number formatting in now based on the __locale parameter specified in the report URL).<br />
<br />
Is this issue fixed in the recent BIRT versions (>2.5.2)?<br />
I think that by default the number formatting should be based on the __locale parameter <br />
<br />
Thanks for your help
mwilliams
I just tried in 3.7.2 and it appears that you can simply do new Number(0.33), in the dynamic text box. Then, when you set the locale parameter to da_DK, in the URL, it changes, correctly, to 0,33. So, this is better, now, it appears.