Displaying decimal numbers - iReport

1

I'm having an unexpected difficulty to display the decimal numbers in iReports, for example if my variable has the following account 180/180 it displays me correctly 1 but if it's 162/180 it only displays 0 to me instead of 0.9, my variable is in the Float format in iReport, does it need any more configuration to display the decimal places?

<textField>
    <reportElement x="523" y="0" width="32" height="20" uuid="9fc5b15c-8007-478e-aef2-edebe1603ab1"/>
    <textElement textAlignment="Center" verticalAlignment="Middle"/>
    <textFieldExpression><![CDATA[$F{centoOitenta} / 180]]></textFieldExpression>
</textField>
    
asked by anonymous 13.10.2017 / 20:39

1 answer

1

The error you noticed happened because cast was cast to int by placing only 180 , instead of 180.0 . Always keep in mind that although JasperReports is a bit confusing it is written in Java so solutions to a common code have great changes to work also in a Field Expression for example.

In order to set the number of decimal places we will have to use a class that provides these resources, I will use here BigDecimal . Put this in your Field Expression:

// Field agora é do tipo string
new BigDecimal($F{field}).divide(new BigDecimal("180.0"), 2, RoundingMode.HALF_UP)

Some websites recommend that you use a string in the constructor to avoid a conversion error but I'm not sure if this is still valid. When it comes to the infinity issue I do not know. Think about how you would display it the way you want it in ordinary Java code.

    
13.10.2017 / 21:42