问题描述
例如:我有9999.99,我想在报告中显示9.999,99。
For example: I have 9999.99 and I want to display 9.999,99 in my report.
我尝试设置自定义模式是#。## 0,00; - #。## 0,00但它不起作用
I try to set custom pattern is #.##0,00;-#.##0,00 but it does not working
我的文本字段:
推荐答案
模式的工作取决于区域设置设置。 分组和十进制分隔符在区域设置中定义。
The work of pattern depends on Locale settings. The Grouping and Decimal separators are defined in Locale.
如果您想要成为没有区域( Locale )设置,您可以使用此scriptlet:
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);
}
}
此scriptlet允许设置模式,自定义分组和十进制分隔符。
This scriptlet allows to set pattern, custom Grouping and Decimal separators.
jrxml 文件:
<?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>
我已设置分组和十进制分隔符和模式( #,## 0.00;#,## 0.00 -
)。
I've set the Grouping and Decimal separators and the pattern (#,##0.00;#,##0.00-
).
结果将是:
注意
不要忘记将带有sriptlet的类(jar)添加到类路径中。
Don't forget to add class (jar) with sriptlet to classpath.
这篇关于如何使用自定义分组和小数分隔符设置数字格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!