回答
工作模式取決於區域設置設置。 分組和十進制分隔符定義在區域設置。
如果你想成爲自由區域(區域設置)設置,您可以使用這個小腳本的:
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);
}
}
這個小腳本允許設置模式,自定義分組和十進制分離。
的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-
)。
結果將是:
注意
不要忘了與sriptlet添加類(JAR)到類路徑中。
非常感謝!但它在碧玉報告中唯一的顯示。當我使用OpenERP插件賈斯珀報告,它仍然顯示分隔符是','你能幫我 – 2013-05-16 08:54:30
你可以檢查在* OpenERP *運行的服務器上的sriptlet的工作?我的意思是* CustomDecimalFormatter.format *方法的工作 – 2013-05-16 09:09:48
@Alex K你能告訴我如何在我的OpenERP Addons中添加我的Java類文件(CustomDecimalFormatter.java)文件。我想添加到我的自定義模塊中。 – 2015-09-28 04:57:54
你可以發佈字段和* textField *聲明(* jrxml *的片段)嗎? – 2013-05-14 09:24:17
你可以看到:http://i.stack.imgur.com/xZySo.png – 2013-05-15 03:15:55
在這種情況下,文本會更好,然後截圖......'$ F {out_balance}'字段的類型是什麼? – 2013-05-15 06:31:59