How to display decimal number - iReport

Asked

Viewed 796 times

1

I’m having unexpected difficulty displaying decimal numbers in iReports, for example if my variable has the following account 180 / 180 it displays me correctly 1 but if it is 162/180 it displays me only 0 instead of 0.9, my variable is in the Float format in iReport, it takes some extra configuration for it 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>
  • Could post jfxml from the field with problems?

  • @Gustavofragoso I changed the question would be this?

1 answer

1


The error you noticed happened because a cast to int occurred while 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 has major changes to work also in a Field Expression for example.

To fix the number of decimal places there we will have to use a class that provides us these resources, I will use here the 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 using a string in the constructor to avoid a conversion error but I’m not sure if it’s still valid. When it comes to the Infinity i don’t know. Think about how you would display the way you want in a common Java code.

  • 2

    Perfect, it worked, a question I can configure to display only at most two decimal places?

  • 1

    I went to test on all the data of the report and some lines where the account is 1/(0/180.0) he is bringing the word infinity you’d know how to fix it?

  • I tried to make a division like this: new BigDecimal($F{quantidade}).divide(new BigDecimal($F{centoOitenta}).divide(new BigDecimal("180.0"), 2, RoundingMode.HALF_UP)) where I want the contents $F{quantidade} is divided by the result ad division of new BigDecimal($F{centoOitenta}).divide(new BigDecimal("180.0"), 2, RoundingMode.HALF_UP). That’s possible?

  • Places this code in a Java test program inside a println with some random values.

  • I managed to define a pattern account result pro, thanks for the help :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.