Reputation: 434
I am getting some strange negative values in my report, which could be from the database. I want to format fields to display zero when the values are negative in the expression editor. How can this be done?
Upvotes: 0
Views: 7492
Reputation: 22867
You should use the BigDecimal.signum() method for check the sign of BigDecimal.
The expression will be:
<textFieldExpression><![CDATA[$P{bigDecimalValue}.signum() == -1 ? "0" : "Not negative big decimal"]]></textFieldExpression>
The expression for Double is simpler:
<textFieldExpression><![CDATA[$P{doubleValue} < 0 ? "0" : "Non negative double"]]></textFieldExpression>
The sample:
<?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>
The output result generated in Jaspersoft Studio will be:
Upvotes: 3