Numbers and Math
5.1 Numeric Literals
def i = 42 // Integer
def l = 42L // Long
def big = 123456789012345678901234567890 // BigInteger (automatic)
def bi = 42G // BigInteger
def d = 3.14 // BigDecimal (exact decimal arithmetic!)
def dbl = 3.14d // Double
def f = 3.14f // Float
def bd = 3.14G // BigDecimal
def hex = 0xFF, oct = 077, bin = 0b1010
def million = 1_000_000
5.2 Arithmetic Behavior
println 0.1 + 0.2 == 0.3 // true (BigDecimal!), unlike Java doubles
println 7 / 2 // 3.5 (Integer division gives BigDecimal)
println 7.intdiv(2) // 3 (integer division)
println 7 % 2 // 1
println 2 ** 10 // 1024 (power operator)
println 2 ** 0.5 // 1.414... (Double)
println 10.0 / 3 // 3.3333333333 (BigDecimal, scale 10)
println Integer.MAX_VALUE + 1 // overflow wraps (int arithmetic)
5.3 Number Methods (GDK)
3.times { println "Hi $it" } // 0, 1, 2
1.upto(5) { print it } // 12345
10.downto(7) { print it }
0.step(10, 3) { print "$it " } // 0 3 6 9
println 3.7.round(), 3.14159.round(2), (-5).abs(), 9.power(2)
println 17.toBigDecimal().setScale(2)
println "42".toInteger() + "3.5".toBigDecimal() + "10".toLong()
println "42".isInteger(), "4.2".isNumber()
println Math.sqrt(16), Math.max(3, 7), Math.PI
println new Random().nextInt(100), Math.random()
println 255.toString(16), Integer.parseInt("ff", 16)