Revision: 2338
at February 2, 2007 11:47 by hansamann

Updated Code
```//the two most common number classes
def intObj = 5
assert intObj.class.name == 'java.lang.Integer'

def bigDecimalObj = 5.12345
assert bigDecimalObj.class.name == 'java.math.BigDecimal'

//you can explicitly create Long, Float and Double with L / F / D
def longObj = 20L
assert longObj.class.name == 'java.lang.Long'
def floatObj = 40.45F
assert floatObj.class.name == 'java.lang.Float'
def doubleObj = 34.45D
assert doubleObj.class.name == 'java.lang.Double'

//if G is used, it checks if the number has a decimal point, then creates a BigInteger or BigDecimal
def bigIntegerObj = 50G
assert bigIntegerObj.class.name == 'java.math.BigInteger'

//some coercion stuff
//multiplication, addition and substraction between two floats results in a double
def z = 2F * 2F
assert z.class.name == 'java.lang.Double'

//multiplication, addition and substraction with BigInteger or BigDecimal results in BigInteger or BigDecimal
def product = 1.1G * 2 //BigDecimal and Integer
assert product.class.name == 'java.math.BigDecimal'

(3G*4).class.name == 'java.math.BigInteger' //BigInteger and Integer

//multiplication of two integers
assert (5*4).class.name == 'java.lang.Integer'

//multiplication with Long
assert (1L*4).class.name == 'java.lang.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
assert 1/2 == 0.5

//some GDK methods for numbers
//think about a java for loop... you don't need it!
def numbers = []
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```

Revision: 2337
at February 2, 2007 10:27 by hansamann

Initial Code
```//the two most common number classes
def intObj = 5
assert intObj.class.name == 'java.lang.Integer'

def bigDecimalObj = 5.12345
assert bigDecimalObj.class.name == 'java.math.BigDecimal'

//you can explicitly create Long, Float and Double with L / F / D
def longObj = 20L
assert longObj.class.name == 'java.lang.Long'
def floatObj = 40.45F
assert floatObj.class.name == 'java.lang.Float'
def doubleObj = 34.45D
assert doubleObj.class.name == 'java.lang.Double'

//if G is used, it checks if the number has a decimal point, then creates a BigInteger or BigDecimal
def bigIntegerObj = 50G
assert bigIntegerObj.class.name == 'java.math.BigInteger'

//some coercion stuff
//multiplication, addition and substraction between two floats results in a double
def z = 2F * 2F
assert z.class.name == 'java.lang.Double'

//multiplication, addition and substraction with BigInteger or BigDecimal results in BigInteger or BigDecimal
def product = 1.1G * 2 //BigDecimal and Integer
assert product.class.name == 'java.math.BigDecimal'

(3G*4).class.name == 'java.math.BigInteger' //BigInteger and Integer

//multiplication of two integers
assert (5*4).class.name == 'java.lang.Integer'

//multiplication with Long
assert (1L*4).class.name == 'java.lang.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
assert (1/2).toString() == '0.5'

//some GDK methods for numbers
//think about a java for loop... you don't need it!
def numbers = []
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```

Initial URL

Initial Description

Initial Title
`Groovy Series: Numbers`

Initial Tags

Initial Language
`Groovy`