16. Numbers

There are two main types of numbers you’re likely to need:

  • Integers (whole numbers)
  • Decimals

Groovy also gives us scientific notation and other number systems and we’ll take a look at how you use them.

Integers

Integers are whole numbers and can be negative or positive:

Using Integers
def age = 27
def coldDay = -8

Groovy will also handle very large numbers:

Large numbers
// 1 astronomical unit (au)
def distanceEarthToSun = 149597870700
def distanceNeptuneToSun = distanceEarthToSun * 30

Decimals

Decimal numbers provide a fraction and can be negative or positive:

Using decimals
def pi = 3.14159

// Measured in celsius
def absoluteZero = -273.15

Scientific notation

Base-10 (decimal) scientific notation (a * 10^b) can also be used by placing an e or E before the exponent:

Using SN
def atomicMass = 1.67e-27

The next example sets the au variable to 1.49597870700 * 10^{11} and then checks to make sure I haven’t messed up the exponent:

Just a check
def au = 1.49597870700e11
assert au == 149597870700

In the previous two examples you can see a signed (positive or negative) integer as the exponent:

  • e-27 is negatively signed
  • e11 can also be written as e+11 and is positively signed

Number Systems

Most of the time we deal with decimal (base-10) numbers but there are other number systems out there. If we want to use the number 15 in base-10 we just type 15 but we can also use:

  • Binary (base-2) by prefixing 0b
    • That’s a zero followed by lower-case “b”
  • Octal (base-8) by prefixing 0
    • That’s just zero
  • Hexadecimal (base-16) by prefixing 0x
    • That’s a zero followed by lower-case “x”

The code below illustrates the many faces of the number 15 (base-10):

Different number systems
println 0b1111    //Binary
println 15         //Decimal
println 017       //Octal
println 0xf       //Hexadecimal

To help you deal with long numbers Groovy lets you use underscores (_) to visually break up the number without changing its value:

Formatting large numbers
assert 1_000_000 == 1000000
assert 0b0001_0110_1101 == 365

Let’s close with a joke:

Lolz
def value = 0b10

println "There are only $value types of people in the world - those who know bin\
ary and those who don't"

Useful Methods and Properties

Groovy (Java) numbers trace their lineage (inherit) back to java.lang.Number. The Number class provides methods to covert between different types of numbers (integer, decimal etc) - we’ll cover this in the chapter on Data Types.

Most numerical classes (e.g. Integer) provide the handy max and min methods that let you compare two numbers of the same numerical type:

max and min
assert Integer.max(10, 2) == 10
assert Integer.min(10, 2) == 2