java - 如何在 Jasper Reports 中为小数指定 RoundingMode

标签 java jasper-reports rounding

我将 Java 与 Jasper Reports 一起使用,并希望使用此格式掩码 "#,##0.00" 来格式化十进制值。乍一看一切都很好,但我发现我的十进制值是使用 RoundingMode.HALF_EVEN 舍入模式舍入的,这在我的情况下是不正确的。

是否可以指定其他舍入模式(我需要 HALF_DOWN 模式)?

最佳答案

您可以使用scriptlets 机制

样本

  • Java 类
package utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;

public class RoundingHelper {

    public static String round(BigDecimal value, RoundingMode mode, String pattern) {
        DecimalFormat format = new DecimalFormat(pattern);
        format.setRoundingMode(mode);
        return format.format(value);
    }
}
  • 报告模板:
<?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.RoundingHelper"/>
    <queryString>
        <![CDATA[SELECT id, (cost/10) 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" verticalAlignment="Middle">
                    <font isBold="true"/>
                </textElement>
                <text><![CDATA[Original value]]></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" verticalAlignment="Middle">
                    <font isBold="true"/>
                </textElement>
                <text><![CDATA[Using RoundingMode.HALF_DOWN]]></text>
            </staticText>
            <staticText>
                <reportElement x="345" 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" verticalAlignment="Middle">
                    <font isBold="true"/>
                </textElement>
                <text><![CDATA[Using RoundingMode.HALF_DOWN]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="20" splitType="Stretch">
            <textField pattern="#,##0.000000000000">
                <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[RoundingHelper.round($F{COST}, RoundingMode.HALF_DOWN, "#,##0.")]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="345" 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[RoundingHelper.round($F{COST}, RoundingMode.HALF_UP, "#,##0.")]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>
  • 结果将是(在 iReport 中预览)

The report in iReport

另一种解决方案

另一种方法是使用 BigDecimal.setScale(int, java.math.RoundingMode) 方法(对于 double 字段):

<textFieldExpression class="java.lang.String"><![CDATA[new BigDecimal($F{COST}).setScale(0, BigDecimal.ROUND_HALF_DOWN).toString()]]></textFieldExpression> 

或仅(对于 BigDecimal 字段):

<textFieldExpression class="java.lang.String"><![CDATA[$F{COST}.setScale(0, BigDecimal.ROUND_HALF_DOWN).toString()]]></textFieldExpression> 
    

有关 JR 中的 scriptlet 的更多信息:Scriptlets

关于java - 如何在 Jasper Reports 中为小数指定 RoundingMode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16899298/

相关文章:

java - @ManyToOne 和@BatchSize

java - 从 PostgreSQL 数据库插入图像

sql-server - SQL Server 按每小时日期时间计数进行分组?

java - 在 java 应用程序中禁用 jaspersoft 日志记录

java - BigDecimal HALF_UP 舍入问题

尽管格式选项甚至在公式中,Excel 都会将 3 个十进制数字更改为完整数字

java - 不同的随机数

java - 模式验证安卓

java - @ConfigurationProperties在spring中的使用

java - 我的 jasper 报告总是覆盖之前的报告 我想每次都用不同的名称保存我的 jasper 报告