Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use integer division operator "/" in groovy?

Tags:

java

groovy

I was migrating a java project to groovy. Then I got an if condition is giving false in Groovy which was giving true in Java code.

Groovy Sample Code:

int status_num = 301
if (status_num / 100 == 3) {
    throw new GoogleServiceConditionException("Google Search System is under maintenance")
}

The if condition is giving false for groovy. For Java, it gives true.

How can I mitigate the issue?

like image 771
SkyWalker Avatar asked Dec 23 '22 06:12

SkyWalker


1 Answers

I have searched over Groovy documentation. It say's like below:

For integer division like in Java, you should use the intdiv() method, as Groovy doesn’t provide a dedicated integer division operator symbol.

So, I have changed the code like below.

if (status_num.intdiv(100) == 3) {
    throw new GoogleServiceConditionException("Google Search System is under maintenance")
}

Now, it works fine.

For more, you can go through the tutorial: The case of the division operator

like image 148
SkyWalker Avatar answered Jan 05 '23 04:01

SkyWalker