Friday, 6 December 2013

How to format number when running report from Java code in iReport

There is always ambiguity that how could you format the data conditionally or at runtime of the report based upon the data in the iReport.

iReport does provides a robust methodology to format the values of the textfield but sometime you require data to be formatted conditionally or needs to be formatted not at the design time but needs to be modified dynamically, that is, based upon the data coming from the datasource or upon the user choice. So this particular article is dedicated to cater this very problem.

The formatting done in the iReport is report specific it like extra utility , of providing the pattern provided by the iReport. When you run that in java code the formatting is removed. So what you can do for the java code is that you could use:
 new java.text.DecimalFormat("pattern").format(put the field here)

So to provide similarity in the report and in the Java code what can be done is that you could replace the expression of the text field with the same expression and the discrepancy could be easily taken care off.In that case the expression would look something like this
 new java.text.DecimalFormat("¤ #,##0").format($F{VALUE})   
Now here the in the field expression easily , any field or parameter could be put and same would work fine for java code too,
Here any pattern can be put, and formatting could be applied to any described field.

Cheers Happy Coding....!!

1 comment:

  1. Hey Ankur! I'm having trouble creating tables in jasper studio (5.6.1) using java beans. I want the columns of the table to be defined by my java bean and not through sql(or database). I'm successfully get those attributes to be used/displayed in my report but I'm still not able to use these as columns for the table. Is this possible at all? If yes, can you please show how? Thanks!


Please post your queries or suggestions here!!