giobby Posted October 14, 2009 Share Posted October 14, 2009 Hi everybody,I must doing this operation in a text field:varMain //from main reportvarSub //from sub reportif (varMain != null && varSub !=null) then print varMain.add(varSub)else if (varMain != null && varSub ==null) then print varMainelse if (varMain == null && varSub !=null) then print varSubelse 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 More sharing options...
mdahlman Posted October 14, 2009 Share Posted October 14, 2009 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 More sharing options...
giobby Posted October 14, 2009 Author Share Posted October 14, 2009 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 More sharing options...
iancr01 Posted October 15, 2009 Share Posted October 15, 2009 No. ".doubleValue" is of type "double" (lower case d). "Double" (upper case D) is a different data type. Link to comment Share on other sites More sharing options...
giobby Posted October 20, 2009 Author Share Posted October 20, 2009 understand thanks Giovanni...La puissance est rien sans controle... Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now