Search

Dark theme | Light theme

January 19, 2010

Groovy Goodness: Integer Divisions

In Groovy we can use two methods to divide two integers. The div() will return the result as BigDecimal. And if we use the method intdiv() we get the result as Integer, because an integer division is done.

def x = 26
def y = 10

def resultDiv = x.div(y)  // or x / y
def resultIntDiv = x.intdiv(y)

assert 2.6 == resultDiv
assert 2 == resultIntDiv
assert java.math.BigDecimal == resultDiv.class
assert java.lang.Integer == resultIntDiv.class