# error : divide by zero

## Recommended Posts

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?

• Replies 3
• Created

#### Popular Days

May you use somethig like this

 Code:
`\$V{B} != 0 ? \$V{A}/\$V{B} * 100 : \$V{some_other_value}`
##### Share on other sites

Hi all..

Thanks Nick.. :)

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?

##### Share on other sites

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.