Loading...

October 19, 2009

Groovy Goodness: BigDecimal

In Groovy java.math.BigDecimal is the default type for non-integer floating types. Because of default imports we don't have to import the BigDecimal class, but we can use it right away. To create a BigDecimal (or BigInteger) we can use the "G" or "g" suffix.

assert 123g instanceof BigInteger
assert 42G instanceof BigInteger
assert 1.423 instanceof BigDecimal
assert 42.0g instanceof BigDecimal
assert 9203.10291G instanceof BigDecimal

// Let's do some math.
assert 3.2 == 1.2G + 2G
assert 10 == 10.1g - 0.1G