当负数包含括号时,如何使字符串值总结为 Big.Decimal?

How to get string values to sum up as Big.Decimal when negative numbers contains parentheses?

我有一份报告,我需要在其中汇总给定列的所有值。 由于需要将负数视为“(123.45)”,将零视为“-”,因此将这些值转换为 char(字符串)。

因为它是用 SQL 写的 我试过将大小写转换为小数或数字,但它给出了错误无效数字,这样我创建的用于求和小数的变量就可以工作,现在那些求和为字符串设置了变量,它不会完成这项工作。

例如,当预览报告时,有一个 column/field 的值:

504.22
(179)
311.67
-
1,345.78

我无法在汇总带中对这些值求和,(179) 为负 -179。

要解析括号中包含负数的字符串,您可以将 java.text.DecimalFormat 与模式 "#,##0.00;(#,##0.00)" 一起使用,但是这不能将 "-" 解析为 0

您将需要使用三元表达式来处理这种特殊情况,因此如果 "-" return 0 否则解析(假设您的字符串编号字段称为 number) .

$F{number}.equals("-")?0:new java.text.DecimalFormat("#,##0.00;(#,##0.00)").parse($F{number})

现在只需将它推入您的变量表达式

<variable name="SumStringNumber" class="java.math.BigDecimal" calculation="Sum">
    <variableExpression><![CDATA[$F{number}.equals("-")?0:new java.text.DecimalFormat("#,##0.00;(#,##0.00)").parse($F{number})]]></variableExpression>
    <initialValueExpression><![CDATA[0]]></initialValueExpression>
</variable>

完整的 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="SumStringNumber" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="afa29477-d3db-4d2a-921d-fded6f024c27">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="StringNumbers"/>
    <queryString language="csv">
        <![CDATA[]]>
    </queryString>
    <field name="number" class="java.lang.String"/>
    <variable name="SumStringNumber" class="java.math.BigDecimal" calculation="Sum">
        <variableExpression><![CDATA[$F{number}.equals("-")?0:new java.text.DecimalFormat("#,##0.00;(#,##0.00)").parse($F{number})]]></variableExpression>
        <initialValueExpression><![CDATA[0]]></initialValueExpression>
    </variable>
    <detail>
        <band height="20" splitType="Stretch">
            <property name="com.jaspersoft.studio.unit.height" value="px"/>
            <textField>
                <reportElement x="0" y="0" width="100" height="20" uuid="472295d5-70de-43d6-b03f-df0452614a39">
                    <property name="com.jaspersoft.studio.unit.height" value="px"/>
                </reportElement>
                <textElement textAlignment="Right" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$F{number}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="100" y="0" width="100" height="20" uuid="20854419-7c55-43bd-86bf-b9dc98c5d51d"/>
                <textElement textAlignment="Right" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$F{number}.equals("-")?0:new java.text.DecimalFormat("#,##0.00;(#,##0.00)").parse($F{number})]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <summary>
        <band height="20" splitType="Stretch">
            <property name="com.jaspersoft.studio.unit.height" value="px"/>
            <textField pattern="#,##0.###;(#,##0.###)">
                <reportElement x="100" y="0" width="100" height="20" uuid="9851d330-1be1-423d-9d01-1a9972ce6a3b">
                    <property name="com.jaspersoft.studio.unit.height" value="px"/>
                </reportElement>
                <textElement textAlignment="Right" verticalAlignment="Middle">
                    <font isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA[$V{SumStringNumber}]]></textFieldExpression>
            </textField>
            <line>
                <reportElement x="0" y="0" width="200" height="1" uuid="2089772d-98f5-4906-b026-69211dfd0e99"/>
            </line>
        </band>
    </summary>
</jasperReport>

输出(我的任意数据)

In general, it's better to pass data in correct format to jasper report, hence in your case it should be passed as Number not as a String and then do the formatting in jasper-report (see example above how pattern is applied to the total sum). This will ensure correct data type when you export for example to excel etc.