siti141283 Posted May 31, 2007 Share Posted May 31, 2007 Hi, I have a problem where i need to calculate percentage. I use this syntax to calculate the percentage, $V{A}/$V{B} * 100. The problem occur if $V{B} is 0. Anyone, please advise me how to solve this problem. can i do any kind of checking like if..else statement?Thanks in advance... :unsure: Link to comment Share on other sites More sharing options...
svenn Posted May 31, 2007 Share Posted May 31, 2007 Used a conditional expression ($V{B} != 0 ? ($V{A}/$V{B} * 100) : 0 ) Link to comment Share on other sites More sharing options...
siti141283 Posted June 1, 2007 Author Share Posted June 1, 2007 Hi all.. Thanks to svenn.. :) I've tried something similar to the code u gave me, that is : new Double(($V{B}.intValue() == 0) ? 0.00 : ($V{A}.intValue() / $V{B}.intValue() * 100)) $V{A} and $V{B} is integer type, so that i cast it to double. That code works perfectly for some of my textfields. But somehow i dont know why it didn't work for some other textfields, somehow it display 0.00 even both variables is not null. Does anyone have any idea why it didn't work? to check if my code is correct or not, i try to use something like : new Double(($V{B}.intValue() == 0) ? 1.00 : 2.00), the value for $V{B} is 316, so when i run it, it display 2.00. So i know it can confirm that the code is correct. It's only when i put the calculation, it'll display 0.00 again. I have similar textfields to display the percentage, but it is for variables with double data type. The code i use is : new Double(($V{C}.doubleValue() == 0.00) ? 0.00 : ($V{D}.doubleValue() / $V{C}.doubleValue() * 100)). That code works fine with all the textfields. Can anyone help me to figure out why is this happening? Link to comment Share on other sites More sharing options...
siti141283 Posted June 1, 2007 Author Share Posted June 1, 2007 Hi all, Finally i'm able to solve the problem. Instead of using $V{A}.intValue() and $V{B}.intValue() for the calculation, i use $V{A}.doubleValue() and $V{B}.doubleValue(), and it works perfectly for all my textfields. ;) Link to comment Share on other sites More sharing options...
satragyaha Posted June 13, 2007 Share Posted June 13, 2007 thanks for your advice. Link to comment Share on other sites More sharing options...
ggiraud Posted June 14, 2007 Share Posted June 14, 2007 Hi,In many programming languages, when you divide an integer by an integer the result is an integer. That's why you didn't have the expected results with intValue() and that's why it's working with doubleValue(). Link to comment Share on other sites More sharing options...
lovelfra Posted January 10, 2023 Share Posted January 10, 2023 It works, thanks 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