Conveniently for exact decimal number calculations, Groovy choses java.lang.BigDecimal as its decimal number type. In addition, both float and double are supported, but require an explicit type declaration, type coercion or suffix. Even if BigDecimal is the default for decimal numbers, such literals are accepted in methods or closures taking float or double as parameter types.
Decimal numbers can’t be represented using a binary, octal or hexadecimal representation.
Underscore in literals
When writing long literal numbers, it’s harder on the eye to figure out how some numbers are grouped together, for example with groups of thousands, of words, etc. By allowing you to place underscore in number literals, it’s easier to spot those groups:
1 2 3 4 5 6 7 8
long creditCardNumber = 1234_5678_9012_3456L long socialSecurityNumbers = 999_99_9999L double monetaryAmount = 12_345_132.12 long hexBytes = 0xFF_EC_DE_5E long hexWords = 0xFFEC_DE5E long maxLong = 0x7fff_ffff_ffff_ffffL long alsoMaxLong = 9_223_372_036_854_775_807L long bytes = 0b11010010_01101001_10010100_10010010
Number type suffixes
我们可以通过添加后缀的方式强制指定一个数字的类型(包含二进制,八进制和十六进制)
1 2 3 4 5 6 7
Type Suffix BigInteger G or g Long L or l Integer I or i BigDecimal G or g Double D or d Float F or f
1 2 3 4 5 6 7 8 9 10 11 12 13
assert42I == new Integer('42') assert42i == new Integer('42') // lowercase i more readable assert123L == new Long("123") // uppercase L more readable assert2147483648 == new Long('2147483648') // Long type used, value too large for an Integer assert456G == new BigInteger('456') assert456g == new BigInteger('456') assert123.45 == new BigDecimal('123.45') // default BigDecimal type used assert1.200065D == new Double('1.200065') assert1.234F == new Float('1.234') assert1.23E23D == new Double('1.23E23') assert0b1111L.class == Long // binary assert0xFFi.class == Integer // hexadecimal assert034G.class == BigInteger // octal
// base and exponent are ints and the result can be represented by an Integer assert2 ** 3instanceof Integer // 8 assert10 ** 9instanceof Integer // 1_000_000_000
// the base is a long, so fit the result in a Long // (although it could have fit in an Integer) assert5L ** 2instanceof Long // 25
// the result can't be represented as an Integer or Long, so return a BigInteger assert100 ** 10instanceof BigInteger // 10e20 assert1234 ** 123instanceof BigInteger // 170515806212727042875...
// the base is a BigDecimal and the exponent a negative int // but the result can be represented as an Integer assert0.5 ** -2instanceof Integer // 4
// the base is an int, and the exponent a negative float // but again, the result can be represented as an Integer assert1 ** -0.3f instanceof Integer // 1
// the base is an int, and the exponent a negative int // but the result will be calculated as a Double // (both base and exponent are actually converted to doubles) assert10 ** -1instanceof Double // 0.1
// the base is a BigDecimal, and the exponent is an int, so return a BigDecimal assert1.2 ** 10instanceof BigDecimal // 6.1917364224
// the base is a float or double, and the exponent is an int // but the result can only be represented as a Double value assert3.4f ** 5instanceof Double // 454.35430372146965 assert5.6d ** 2instanceof Double // 31.359999999999996
// the exponent is a decimal value // and the result can only be represented as a Double value assert7.8 ** 1.9instanceof Double // 49.542708423868476 assert2 ** 0.1f instanceof Double // 1.0717734636432956