Na.Q
Na.Q

Reputation: 13

How do I set number format with custom Grouping and Decimal separators

For example: I have 9999.99 and I want to display 9.999,99 in my report.

I try to set custom pattern is #.##0,00;-#.##0,00 but it does not working

My textfield:

enter image description here

Upvotes: 1

Views: 13926

Answers (1)

Alex K
Alex K

Reputation: 22867

The work of pattern depends on Locale settings. The Grouping and Decimal separators are defined in Locale.

If you want be free of regional (Locale) settings you can use this scriptlet:

package utils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;

public class CustomDecimalFormatter {

    public static String format(BigDecimal value, String pattern, char decimalSeparator, char groupingSeparator) {
        DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(Locale.getDefault());
        otherSymbols.setDecimalSeparator(decimalSeparator);
        otherSymbols.setGroupingSeparator(groupingSeparator);
        DecimalFormat df = new DecimalFormat(pattern, otherSymbols);
        return df.format(value);
    }
}

This scriptlet allows to set pattern, custom Grouping and Decimal separators.

The 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="format_decimal" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
    <import value="utils.CustomDecimalFormatter"/>
    <queryString>
        <![CDATA[SELECT id, cost*100 as cost from product]]>
    </queryString>
    <field name="ID" class="java.lang.Integer"/>
    <field name="COST" class="java.math.BigDecimal"/>
    <columnHeader>
        <band height="50">
            <staticText>
                <reportElement x="0" y="0" width="154" height="50"/>
                <box leftPadding="10">
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <textElement textAlignment="Center">
                    <font isBold="true"/>
                </textElement>
                <text><![CDATA[The result of using
classical pattern.
Depends on System locale]]></text>
            </staticText>
            <staticText>
                <reportElement x="154" y="0" width="191" height="50"/>
                <box leftPadding="10">
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <textElement textAlignment="Center">
                    <font isBold="true"/>
                </textElement>
                <text><![CDATA[The result of using the sriptlet]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="20" splitType="Stretch">
            <textField pattern="#,##0.00;#,##0.00-">
                <reportElement x="0" y="0" width="154" height="20"/>
                <box leftPadding="10">
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <textElement/>
                <textFieldExpression class="java.math.BigDecimal"><![CDATA[$F{COST}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="154" y="0" width="191" height="20"/>
                <box leftPadding="10">
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <textElement/>
                <textFieldExpression class="java.lang.String"><![CDATA[CustomDecimalFormatter.format($F{COST}, "#,##0.00;#,##0.00-", ',', '.')]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

I've set the Grouping and Decimal separators and the pattern (#,##0.00;#,##0.00-).

The result will be:

enter image description here


Note

Don't forget to add class (jar) with sriptlet to classpath.

Upvotes: 3

Related Questions