Sunday 5 April 2015

Tip : Convert Decimal to Integer and change its pattern using java expression on a text field in Jasper Studio 6.x or iReport 5.x (Or) Combine text with field value for dynamic space in Jasper Reports

This tip will be useful in below scenarios with Jasper Reports

Sometimes you may need to combine Text with a value coming from database field .
Generally you take two fields for this. Text field for printing text and Filed for field value and set pattern for it and adjust the positions of these two fields. But what if your value dynamically changes.

That is some times you may get 2 digit integer and sometimes you may get 5 digit integer. Do you really get dynamic space b/w two fields ( Text field and Database field).

Problem Image : 




Solution : 
Combine text with field using java expression

1) Get the integer value first by rounding.
 (Generally a double have type java.math.BigDecimal in report tool)
(int) ( $F{sales} + 0.5)

2) Get the comma separated integer from above integer using below syntax
NumberFormat.getIntegerInstance().format(<Integer Value>)

3) Combining both steps 1 & 2

Orders : 70 for $19,436  (Now there will not be any space when combining text with field value)

Example : 
"invoices for"+" "+"\$"+ NumberFormat.getIntegerInstance().format((int) ( $F{sales} + 0.5))


NOTE: 
1) In Jasper reports do display $ sign in an expression one should use backslash character combined in double quotes ( i.e., "\$")

I hope this helps someone :-)

Cheers..!!


No comments:

Post a Comment