Ireport IF Else Expression

ireport

I have this expression in iReport.:

($F{Q3_February}==0)?"-":$F{Q3_February}

Expression class is Double, I want this to returns as "-" if the value of $F{Q3_February} is 0.

I get "-" only if I change it to Expression class String but the problem is if the value is false it doesn't return a value of #,##0.00 %

Best Answer

Your problem is that the textField pattern is not applied if the expression class is not numeric. You are trying to conditionally change not only the value, but also the class. This is not possible with a single textField.

I think your best bet is to separate this into two textFields, one string and one double. Place them on top of each other and then use printWhenExpressions to hide the one that is not wanted. The end result will be similar to having a single field with a conditional expression, but gives you more flexibility with the other element properties (i.e. class and pattern).

Example:

<textField pattern="#,##0.00 %">
    <reportElement x="200" y="80" width="100" height="20">
        <printWhenExpression>
            <![CDATA[$F{Q3_February}!=0]]>
        </printWhenExpression>
    </reportElement>
    <textFieldExpression class="java.lang.Double">
        <![CDATA[$F{Q3_February}]]>
    </textFieldExpression>
</textField>
<textField>
    <reportElement x="200" y="80" width="100" height="20">
        <printWhenExpression>
            <![CDATA[$F{Q3_February}==0]]>
        </printWhenExpression>
    </reportElement>
    <textFieldExpression class="java.lang.String">
        <![CDATA["-"]]>
    </textFieldExpression>
</textField>