Copy this code and paste it in your HTML
//the two most common number classes
def bigDecimalObj
= 5.12345 assert bigDecimalObj.
class .
name == 'java.math.BigDecimal'
//you can explicitly create Long, Float and Double with L / F / D
//if G is used, it checks if the number has a decimal point, then creates a BigInteger or BigDecimal
assert bigIntegerObj.
class .
name == 'java.math.BigInteger'
//some coercion stuff
//multiplication, addition and substraction between two floats results in a double
//multiplication, addition and substraction with BigInteger or BigDecimal results in BigInteger or BigDecimal
def product
= 1.1G
* 2 //BigDecimal and Integer
( 3G
* 4 ) .
class .
name == 'java.math.BigInteger' //BigInteger and Integer
//multiplication of two integers
//multiplication with Long
//dividing some numbers
( 5 /
5 ) .
class .
name == 'java.math.BigDecimal' //if you want an integer, use the intdiv() method
( 5.
intdiv ( 5 ) ) .
class .
name == 'java.lang.Integer' ( 1.
intdiv ( 5 ) ) .
class .
name == 'java.lang.Integer' //result is 0
//some GDK methods for numbers
//think about a java for loop... you don't need it!
10.
times { numbers
<< it
} //it will contain values from 0 to 9 assert numbers.
join ( "," ) == '0,1,2,3,4,5,6,7,8,9' 10.
upto ( 12 ) { numbers
<< it
} assert numbers.
join ( "," ) == '0,1,2,3,4,5,6,7,8,9,10,11,12' 11.downto ( 10 ) { numbers << it}
assert numbers.
join ( "," ) == '0,1,2,3,4,5,6,7,8,9,10,11,12,11,10'
numbers = [ ]
1.
step ( 2 ,
0.1 ) { numbers
<< it;
assert it.
class .
name == 'java.math.BigDecimal' } assert numbers.
join ( "," ) == '1,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9' //notice 2 is not included
numbers = [ ]
1.0.
step ( 2 ,
0.1 ) { numbers
<< it;
assert it.
class .
name == 'java.math.BigDecimal' } assert numbers.
join ( "," ) == '1.0,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9' //notice how the first value changed to 1.0