Jump to content

About multiple conditional expressions...


giobby

Recommended Posts

 

Hi everybody,

I must doing this operation in a text field:

varMain //from main report

varSub //from sub report

if  (varMain != null && varSub !=null)  then  print  varMain.add(varSub)

else if  (varMain != null && varSub ==null)  then  print  varMain

else if (varMain == null && varSub !=null)  then  print  varSub

else print null

 

I tried with this solution...

$V{varMain} !=null ? new BigDecimal($V{varMain}.toString()).add(($V{varSub} ==null ? new BigDecimal("0") : new BigDecimal($V{varSub}.toString()))) : ($V{varSub} ==null ? null : new BigDecimal($V{varSub}.toString()))
 

...and it works.

Question:

Is there a less complicated way to do this kind of operations (especially when the variables to deal are more than 2)?

 thanks in advance

 

Giovanni

...La puissance est rien sans controle...

 



Post Edited by giobby at 10/14/2009 15:01
Link to comment
Share on other sites

  • Replies 4
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Giovanni,

Your technique seems fine. But converting your variables to strings and then to BigDecimals is odd. For example, if they are Doubles then you could use something like this:

new BigDecimal(
    ( $V{varMain}==null ? 0 : $V{varMain}.doubleValue() )
    + ( $V{varSub}==null ? 0 : $V{varSub}.doubleValue() )
)

This is different from yours because it returns 0 when both variables are null. But otherwise I think it is the same. It's about half as much code, and it scales up easily.

Regards,
Matt

Link to comment
Share on other sites

wonderful!

this is more clear than my solution... and also allows me to deal the number the variables that i want. For example:

new BigDecimal(

( $V{varMain}==null ? 0 : $V{varMain}.doubleValue() )

+ ( $V{varSub1}==null ? 0 : $V{varSub}.doubleValue() )

+ ( $V{varSub2}==null ? 0 : $V{varSub}.doubleValue() )

+ ( $V{varSub...n}==null ? 0 : $V{varSub}.doubleValue() )

)

 

is true?

 

And also another question: can I do the same thing declaring "new Double(...)" instead "newBigDecimal(...)" at the beginning to avoid to write ".double value()" all the times (assuming that the type of my vars is Double)?

 

ps. I ask this because i can't do any attemps now. All the project are in another place...

 

thanks a lot!

 

 

Giovanni

...La puissance est rien sans controle...

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...