Wednesday, 11 March 2015

Tips : Playing with variables in Jasper Studio to find percentages, to get the two digits after decimal point & to get the only numeric value from decimal by rounding the value

Software : 
Jasper Design Studio : 6.0.1 Pro
Jasper Server : 6.0.1 Pro

1) Calculating Percentage from java.lang.BigDecimal Types

By default all the numeric fields in report takes java.math.BigDecimal & you can not perform arithmetic  operations like division for calculating percentages. (Alternatively you need to work with the methods from BigDecimal class to get % kind of things)

Find percentage : 
1) Create a variable  of type java.lang.Double
2) Calculation = Nothing
3) Expression :
      $F{Field1}.doubleValue()/$F{Filed2}.doubleValue())*100  

    NOTE : $F{Field1} and $F{Filed2} are of java.math.BigDecimal types .
4) Increment type : None
5) Reset group :  If you are creating variables in any data set & using it in table component give your GROUP Name

6) Drag the variable on to the design area of table component.
7) Double click the variable to open it's  expression
     a) By default you can see it's expression as $V{Variable_1}
     b) Change the variable expression  to
              new DecimalFormat("#.##").format(Double.valueOf($V{Variable_1}))+"%"

2) Two decimal places after floating point for java.math.BigDecimal type values

  $V{Variable_2}.setScale(2, java.math.RoundingMode.HALF_UP)+"%"
This is on dragged variable. Not in the variable directly. The variable initially $V{Variable_2}. Double click it to get it's expression editor

Example: 95.83%

NOTE : 
a) Create a variable of double type for finding %'s .
                   i.e.,
                       Variable Name : VAR_PER_1
                      Expression :  (a.doubleValue()/b.doubleValue()) *100
                      Example : 95.834444123

b)  Now convert the above variable to Decimal type by creating a new variable of type Decimal
                 i.e., Variable Name : VAR_PER_2
                 BigDecimal.valueOf($V{VAR_PER_1})    [Remember it VAR_PER_1]

c) Now get 2 decimal places  by applying   $V{VAR_PER_2}.setScale(2, java.math.RoundingMode.HALF_UP)+"%"


3) Get only  Numeric value as percentage by rounding the decimal places of Double value

Assume you have java.lang.Double type having 45.45 and want to get 46 from it. Then the below expression will help full to you.

Variable_1=45.45                                              of java.lang.Double type
Variable_2= (int) ($V{Variable_1} + 0.5)        of java.lang.Integer type

[i.e., you need to create another variable of integer type and in its expression type cast the double variable to integer]

There could be another ways to solve the same kind of solutions using java expressions but I hope somehow the above tips help full.

Thanks for reading this page & suggestions and improvements to this page are welcome in comments box :-)

:-) SADAKAR POCHAMPALLI :-)
BI DEVELOPER, HYDERABAD, INDIA.

No comments:

Post a Comment