IReport 3.0.0: How to compare big decimals in field expression

bigdecimalexpressionireportjasper-reports

Can anyone tell me how to compare two Big Decimal values in field expression (textFieldExpression)? I'm using iReport 3.0.0.

What is the syntax for greater then?

This is my expression, and it works but only shows if two values not equal, and I need to check which one is greater (amount > paid_amount).

I'm using this expression:

($F{paid_date}!=null & ($F{amount}.equals($F{paid_amount}))) ? new String ("PAID") :(
($F{paid_date}==null) ? new String ("NOT PAID"): (
($F{paid_date}!=null & (!$F{amount}.equals($F{paid_amount}))) ? new String ("PARTIALLY PAID"):(new String ("INVOICE MISSING "))))

Best Answer

For fields like this:

<field name="amount" class="java.math.BigDecimal"/>
<field name="paid_amount" class="java.math.BigDecimal"/>

you should use this expression:

<textFieldExpression class="java.lang.String"><![CDATA[$F{amount}.compareTo($F{paid_amount}) == 1 
? "The amount value is greater than the paid_amount value" 
: $F{amount}.compareTo($F{paid_amount}) == 0 ? 
"The amount value is equal to the paid_amount value" : 
"The amount value is less than the paid_amount value"]]></textFieldExpression>

This expression based on using int BigDecimal.compareTo(java.math.BigDecimal val) method.