Sunday 13 January 2019

Groovy: Arithmetic Operators


Below table summarizes the Arithmetic operators provided by Groovy.

Operator
Performs
Example
+
Addition
a + b
-
Subtraction
a - b
*
Multiplication
a * b
/
Division
a / b
%
Remainder
a % b
**
Power
a ** b

HelloWorld.groovy
a = 10
b = 3

println "a =  ${a}"
println "b = ${b}"
println "a + b = ${a + b}"
println "a - b = ${a - b}"
println "a * b = ${a * b}"
println "a / b = ${a / b}"
println "a % b = ${a % b}"
println "a ** b = ${a ** b}"


Output
a =  10
b = 3
a + b = 13
a - b = 7
a * b = 30
a / b = 3.3333333333
a % b = 1
a ** b = 1000


As you see the output pf a/b, it returns a double value. Some times you may want only integer result as part of the division, in that case use intdiv method (a.intdiv(b)).


Previous                                                 Next                                                 Home

No comments:

Post a Comment