rainman
rainman

Reputation: 2649

Unable to format number in Jasper Reports

I am designing a PDF report with iReport 3.7.6, using an XML datasource which contains some prices (Money value), which are introduced a textField in my report, so I used the pattern textField property to set the currency format, but unfortunately it does not format it as i want.

Here you can see the value I want to format as currency in the XML file:

<importFormalitzat>1500.0</importFormalitzat>

This is the JRXML code for the textField where I display the value:

<textField pattern="¤ #,##0.00" isBlankWhenNull="true">
    <reportElement x="4" y="475" width="181" height="13"/>
    <textElement lineSpacing="Single">
       <font size="8"/>
    </textElement>
    <textFieldExpression class="java.lang.Double"><![CDATA[$F{importFormalitzat} ]]></textFieldExpression>
</textField>

As you can see the pattern I am using to format the value is: pattern="¤ #,##0.00"

But the result I get when I preview the report is this: 15000,00

I don't understand why I get 15000,00 while the value I have in the XML file from where I am filling the report is 1500.0.

I tried to change the Java class in the TextField expression to Integer, BigDecimal, but I still do not get the correct value in the report!

Upvotes: 1

Views: 4266

Answers (1)

Petter Friberg
Petter Friberg

Reputation: 21710

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

Upvotes: 1

Related Questions