I have a string with some numeric value.
I want to format it in a way where hundreds are comma separated and the number is having $ dollar sign before it.
e.g. 12345 should be formatted to $ 12,345.00
I tried the below code without dollar sign:
new java.text.DecimalFormat(#,##0.00).format.(myString)
and the below one with dollar sign:
new java.text.DecimalFormat($ #,##0.00).format.(myString)
However, both are giving error.
What is the right way to achieve this format ?
This is a part of jasper report jrxml where I want to avoid "null" on the report and thus inserting the below code:
<textField isBlankWhenNull="false" isStretchWithOverflow="true">
<reportElement stretchType="RelativeToTallestObject" x="1350" y="0" width="150" height="30"/>
<textElement/>
<textFieldExpression class="java.math.BigDecimal"><![CDATA[$F{myString}!=null?new java.text.DecimalFormat(#,##0.00).format.($F{myString}):"Unavailable"]]></textFieldExpression>
</textField>
Where myString results from a query and is declared in jrxml as:
<field name="myString" class="java.lang.String"/>
Earlier myString was declared as BigDecimal, but then comparison operator ?= was not working.
If the currency value is not available, I want to print "unavailable" on the report instead of default "null". Else, I want the number to be properly formatted as described above.
How to resolve this issue?
Thanks for reading.
You can do this also using the iReport Studio. In the studio click on the field and see the properties pane.In properties pane under Text Field Properties you'll find the Pattern property. Paste #,##0.00 or click on the three dots and tick the check box in the popup menu to separate at 1000.
The correct expression is:
The working sample for java.lang.Integer and java.lang.String:
The result will be (preview in iReport):
Note: You should also add check for null.
You can also use pattern property of textField for formatting data.
The sample:
The result will be the same.
Just came accross this Problem and found a solution which is working fine for me.
Be Aware - its not exactly what the Autor is asking for but if you google this Problem you will end up here.
I have Servers with German and English Locale and on all the Currency should be like
7.500,60
My final expression:
so the Locale Setting is "hardcoded" - exactly what i needed.
Maybe this will help someone
Im using iReport 5.6.0 and I took some of these answers but what It worked for me was:
Text Field (I put this into the designer):
And in the Pattern (into properties tab), I used Custom Format with:
Putting all the string mentioned here in a whole "Edit expression" field didnt worked for me.
Hope It helps.
PD: I was using groovy jar that is the compatible with jasper 5.6.0 checked in mvnrepository site.
The above code can work for three cases:
For me i wanted to put "," comma after every three digits (by passing string as a parameter) and i tried the following code. It did work for me.... Thank you very much. I appreciate this answer.
When trying the recommended answer in iReport, I got "illegal string body character after dollar sign;" when I ran the report.
Easily remedied by escaping the dollar sign with a backslash like so: