gpt4 book ai didi

java - 如何检查JasperReports中BigDecimal字段的值是否为负数

转载 作者:行者123 更新时间:2023-12-01 21:09:07 25 4
gpt4 key购买 nike

我在报告中收到一些奇怪的负值,这些值可能来自数据库。我想将字段格式化为当表达式编辑器中的值为负数时显示零。如何做到这一点?

最佳答案

您应该使用 BigDecimal.signum() 检查 BigDecimal 符号的方法。

表达式为:

<textFieldExpression><![CDATA[$P{bigDecimalValue}.signum() == -1 ? "0" : "Not negative big decimal"]]></textFieldExpression>

Double 的表达式更简单:

<textFieldExpression><![CDATA[$P{doubleValue} < 0 ? "0" : "Non negative double"]]></textFieldExpression>

示例:

<?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="BigDecimal check" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<parameter name="doubleValue" class="java.lang.Double" isForPrompting="false">
<defaultValueExpression><![CDATA[1234.567]]></defaultValueExpression>
</parameter>
<parameter name="bigDecimalValue" class="java.math.BigDecimal" isForPrompting="false">
<defaultValueExpression><![CDATA[new BigDecimal(-9.8)]]></defaultValueExpression>
</parameter>
<title>
<band height="70">
<textField>
<reportElement x="10" y="10" width="300" height="15"/>
<textFieldExpression><![CDATA[$P{doubleValue} < 0.0 ? "0" : "Non negative double"]]></textFieldExpression>
</textField>
<textField>
<reportElement x="10" y="25" width="300" height="15"/>
<textFieldExpression><![CDATA[$P{bigDecimalValue}.signum() == -1 ? "0" : "Not negative big decimal"]]></textFieldExpression>
</textField>
</band>
</title>
</jasperReport>

Jaspersoft Studio 中生成的输出结果为:

enter image description here

关于java - 如何检查JasperReports中BigDecimal字段的值是否为负数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41590373/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com