Formatting a String to a Currency Format in Jasper Report

formatting a string to a currency format in jasper report

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.

Formatting currency in Jasper Reports using pattern

You were close

<textField pattern="¤ #,##0.00">

should work.

You need the "¤" character to make it a "currency" format.

If you download iReports with a version number that matches your server you find out what your options are.

Format String to Number iReport

You should set $V{SumPrice} "Variable class" property to java.lang.Integer and "Variable Expression" property to Integer.parseInt($F{Price}).

How do I format a number as 2.564.894.621 in JasperSoft Studio?

The best way to format in jasper report is to use the pattern attribute on the textField tag. This will keep correct class (Number), when exporting to for example excel, excel can identify it as number and will also apply same pattern.

Properties >> TextField >> Pattern

Either you know the correct pattern or you use the IDE to help you generate it

IDE

jrxml result

<textField pattern="#,##0">
<reportElement x="0" y="0" width="200" height="25" uuid="ee49d149-394b-4ac6-a0a2-6d207b0c8d89"/>
<textElement>
<font fontName="DejaVu Serif" size="14"/>
</textElement>
<textFieldExpression><![CDATA[$F{myNumber}]]></textFieldExpression>
</textField>

And exporting with a Locale that uses . as grouping separator it will display

Result

if your result is with the grouping separator , this does not depend on the pattern but simply your locale see this: How to invert the comma and dot when number formatting

In JasperSoft Studio the locale used during preview can be set in

Window>>Preferences>>Report Execution: Locale

Note: expression like

<textFieldExpression><![CDATA[new DecimalFormat("#,##0").format($F{myNumber})]]></textFieldExpression>

can be used as well but its better to avoid since the export manager will treat this as text only

Unable to format number in Jasper Reports

This is a problem of Locale and Number Pattern when you load your xml file, hence with your "Spanish" Locale the decimal separator is , not .

You need to set correct Locale and the number pattern to your xml data source:

In java

JRXmlDataSource ds = new JRXmlDataSource("theXmlFile");
ds.setLocale(Locale.US); //Example US that uses . as decimal separator
ds.setNumberPattern("###0.00;-###0.00");//Pattern of number in xml file

In iReport (definition of the datasource)

The datasource

EDIT: Adding running example with its output

XML File

<report>
<persons>
<person>
<name>John</name>
<value>1500.0</value>
</person>
<person>
<name>Marko</name>
<value>1700.0</value>
</person>
</persons>
</report>

JRXML File

<?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="TableWithList" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="2347c131-1884-430a-b77f-59f08f896c8a">
<queryString language="xPath">
<![CDATA[/report/persons/person]]>
</queryString>
<field name="value" class="java.lang.Double">
<fieldDescription><![CDATA[value]]></fieldDescription>
</field>
<field name="name" class="java.lang.String">
<fieldDescription><![CDATA[name]]></fieldDescription>
</field>
<columnHeader>
<band height="20">
<staticText>
<reportElement x="0" y="0" width="100" height="20" uuid="a131c9e4-a295-460f-8d9a-d7a85f0de41a"/>
<textElement verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<text><![CDATA[name]]></text>
</staticText>
<staticText>
<reportElement x="100" y="0" width="100" height="20" uuid="e39eb5aa-0687-48e8-a268-193993d647e1"/>
<textElement verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<text><![CDATA[value]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<textField pattern="¤ #,##0.00">
<reportElement x="100" y="0" width="100" height="20" uuid="7c293632-3735-497e-8315-72ade69125e9"/>
<textElement verticalAlignment="Middle"/>
<textFieldExpression class="java.lang.Double"><![CDATA[$F{value}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="0" y="0" width="100" height="20" uuid="83e733ca-8ad8-462c-b1b0-85b3c8b3e6f1"/>
<textElement verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>

NOTE: The field definition for value class="java.lang.Double"

Output

Output

Some currency Symbol in Jasper report is not coming

I believe that your issue related with font supporting.

We should use Font Extensions in case using JRPdfExporter.

I tried to use font with Chinese support and in this case everything is OK.I don't know why using a ton of another fonts is not helping

Example

Java code

Map<String, Object> params = new HashMap<>();
params.put(JRParameter.REPORT_LOCALE, Locale.CHINA);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, new JREmptyDataSource());

Report template

There are 4 textFields in jrxml file: two using font with Chinese support and another 2 - without it.

<?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="Show currency" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<parameter name="value" class="java.lang.Double" isForPrompting="false">
<defaultValueExpression><![CDATA[1234.567]]></defaultValueExpression>
</parameter>
<title>
<band height="70">
<textField>
<reportElement x="10" y="10" width="300" height="15"/>
<textElement>
<font fontName="Sharp Dawn"/>
</textElement>
<textFieldExpression><![CDATA[NumberFormat.getCurrencyInstance(new Locale("zh", "CN")).format($P{value})]]></textFieldExpression>
</textField>
<textField>
<reportElement x="10" y="25" width="300" height="15"/>
<textElement>
<font fontName="Sharp Dawn"/>
</textElement>
<textFieldExpression><![CDATA[NumberFormat.getCurrencyInstance($P{REPORT_LOCALE}).format($P{value})]]></textFieldExpression>
</textField>
<textField>
<reportElement x="3" y="40" width="300" height="15"/>
<textFieldExpression><![CDATA[NumberFormat.getCurrencyInstance($P{REPORT_LOCALE}).format($P{value})]]></textFieldExpression>
</textField>
<textField>
<reportElement x="3" y="55" width="300" height="15"/>
<textFieldExpression><![CDATA[NumberFormat.getCurrencyInstance(new Locale("zh", "CN")).format($P{value})]]></textFieldExpression>
</textField>
</band>
</title>
</jasperReport>

Output result

The pdf file generated with help of JRPdfExporter looks like

Sample Image

The Yuan symbol is showing only for first group.



Related Topics



Leave a reply



Submit