Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

formatting a string to a currency format in jasper report

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.

like image 376
Vicky Avatar asked Jun 06 '12 11:06

Vicky


2 Answers

The correct expression is:

new java.text.DecimalFormat("$ #,##0.00").format(Double.valueOf($P{strParam}))

The working sample for java.lang.Integer and java.lang.String:

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="format_as_current" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
    <parameter name="intParam" class="java.lang.Integer">
        <defaultValueExpression><![CDATA[12345678]]></defaultValueExpression>
    </parameter>
    <parameter name="strParam" class="java.lang.String">
        <defaultValueExpression><![CDATA["12345678.95"]]></defaultValueExpression>
    </parameter>
    <title>
        <band height="79" splitType="Stretch">
            <textField>
                <reportElement x="137" y="18" width="291" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[new java.text.DecimalFormat("$ #,##0.00").format($P{intParam})]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="137" y="48" width="291" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[new java.text.DecimalFormat("$ #,##0.00").format(Double.valueOf($P{strParam} != null && $P{strParam}.length() > 0 ? Double.valueOf($P{strParam}) : 0))]]></textFieldExpression>
            </textField>
        </band>
    </title>
</jasperReport>

The result will be (preview in iReport):

The result in *iReport*

Note: You should also add check for null.

You can also use pattern property of textField for formatting data.

The sample:

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="format_as_current" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
    <parameter name="intParam" class="java.lang.Integer">
        <defaultValueExpression><![CDATA[12345678]]></defaultValueExpression>
    </parameter>
    <parameter name="strParam" class="java.lang.String">
        <defaultValueExpression><![CDATA["12345678.95"]]></defaultValueExpression>
    </parameter>
    <title>
        <band height="148" splitType="Stretch">
            <textField pattern="$ #,##0.00">
                <reportElement x="218" y="99" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$P{intParam}]]></textFieldExpression>
            </textField>
            <textField pattern="$ #,##0.00">
                <reportElement x="218" y="119" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$P{strParam} != null && $P{strParam}.length() > 0 ? Double.valueOf($P{strParam}) : 0]]></textFieldExpression>
            </textField>
        </band>
    </title>
</jasperReport>

The result will be the same.

like image 59
Alex K Avatar answered Oct 03 '22 10:10

Alex K


The above code can work for three cases:

  1. fix two zeros after decimal point. When I tried to use "Double" datatype for this purpose it did not work for me. But this code can do it.
  2. Fix $ -sign before the number in string datatype and also takes care of "-" sign in its own.
  3. Get "," after 3 digits in a number

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.

<textFieldExpression>

<![CDATA[new java.text.DecimalFormat("$ #,##0.00").format(Double.valueOf($P{actualWrittenPremium} != null && $P{actualWrittenPremium}.length() > 0 ? Double.valueOf($P{actualWrittenPremium}) : 0))]]>

</textFieldExpression>
like image 39
Rushi Avatar answered Oct 03 '22 09:10

Rushi