Numbers

Numbers in the Puppet language are normal integers and floating point numbers.

You can work with numbers using arithmetic operators.

Numbers are written without quotation marks, and can consist only of:
  • Digits.

  • An optional negative sign (-). This is actually the unary negation operator rather than part of the number. Explicit positive signs (+) aren’t allowed.

  • An optional decimal point, which results in a floating point value.

  • An optional e or E for scientific notation of floating point values.

  • An 0 prefix for octal base, or 0x or 0X prefix hexidecimal base.

Integers

Integers are numbers without decimal points.

If you divide two integers, the result is not a float. Instead, Puppet truncates the remainder. For example:
$my_number = 2 / 3    # evaluates to 0
$your_number = 5 / 3  # evaluates to 1

Floating point numbers

Floating point numbers (“floats”) are numbers that include a fractional value after a decimal point, including a fractional value of zero, as in 2.0.

If an expression includes both integer and float values, the result is a float:
$some_number = 8 * -7.992           # evaluates to -63.936
$another_number = $some_number / 4  # evaluates to -15.984
Floating point numbers between -1 and 1 cannot start with a bare decimal point. They must have a zero before the decimal point:
$product = 8 * .12 # syntax error
$product = 8 * 0.12 # OK
You can express floating point numbers in scientific notation: append e or E, plus an exponent, and the preceding number is multiplied by 10 to the power of that exponent. Numbers in scientific notation are always floats:
$product = 8 * 3e5  # evaluates to 2400000.0

Octal and hexadecimal integers

Integer values can be expressed in decimal notation (base 10), octal notation (base 8), and hexadecimal notation (base 16).

Decimal (base 10) integers (other than 0) must not start with a 0.

Octal (base 8) integers have a prefix of 0 (zero), followed by octal digits 0 to 7.

Hexadecimal (base 16) integers have a prefix of 0x or 0X, followed by hexadecimal digits 0 to 9, a to f, or A to F.

Floats can't be expressed in octal or hexadecimal.

Examples:
# octal
$value = 0777   # evaluates to decimal 511
$value = 0789   # Error, invalid octal
$value = 0777.3 # Error, invalid octal

# hexadecimal
$value = 0x777 # evaluates to decimal 1911
$value = 0xdef # evaluates to decimal 3567
$value = 0Xdef # same as above
$value = 0xDEF # same as above
$value = 0xLMN # Error, invalid hex

Converting numbers to strings

Numbers are automatically converted to strings when interpolated into a string. The automatic conversion uses decimal (base 10) notation.

You can also cast a number into a string directly, by declaring a new String object.

Examples:
$from_integer = String(342)
$from_float   = String(3.14159)

To convert numbers to non-decimal string representations, use the sprintf function.

Converting strings to numbers

Arithmetic operators in an expression automatically convert strings to numbers, but in all other contexts (for example, resource attributes or function arguments), Puppet won’t automatically convert strings to numbers.

To convert a string to a number, cast the type by declaring a new Numeric object.

Examples:
$integer_var = Integer('342')
$float_var   = Float('3.14159')
$numeric_var = Numeric('5280')

For more information about casting, see the function documentation for converting to Integer and converting to Float.

To extract numbers from strings, use the scanf function. This function handles surrounding non-numerical text.

The Integer data type

The data type of integers is Integer. By default, Integer matches whole numbers of any size, within the limits of available memory. You can use parameters to restrict which values Integer matches.

Parameters

The full signature for Integer is:
Integer[<MIN VALUE>, <MAX VALUE>]
These parameters are optional. They must be listed in order; if you need to specify a later parameter, you must also specify values for any prior ones.
Position Parameter Data type Default Description
1 Minimum value Integer negative infinity The minimum value for the integer. This parameter accepts the special value default, which uses its default value.
2 Maximum value Integer infinity The maximum value for the integer. This parameter accepts the special value default, which uses its default value.

Practically speaking, the integer size limit is the range of a 64-bit signed integer (−9,223,372,036,854,775,808 to 9,223,372,036,854,775,807), which is the maximum size that can roundtrip safely between the components in the Puppet ecosystem.

Examples:
Integer
Matches any integer.
Integer[0]
Matches any integer greater than or equal to 0.
Integer[default, 0]
Matches any integer less than or equal to 0.
Integer[2, 8]
Matches any integer from 2 to 8, inclusive.

The Float data type

The data type of floating point numbers is Float. By default, Float matches floating point numbers within the limitations of Ruby's Float class. Practically speaking, this means a 64-bit double precision floating point value. You can use parameters to restrict which values Float matches.

Parameters

The full signature for Float is:
Float[<MIN VALUE>, <MAX VALUE>]
These parameters are optional. They must be listed in order; if you need to specify a later parameter, you must also specify values for any prior ones.
Position Parameter Data type Default Description
1 Minimum value Float negative infinity The minimum value for the float. This parameter accepts the special value default, which uses its default value.
2 Maximum value Float infinity The maximum value for the float. This parameter accepts the special value default, which uses its default value.
Examples:
Float
Matches any floating point number.
Float[1.6]
Matches any floating point number greater than or equal to 1.6.
Float[1.6, 3.501]
Matches any floating point number from 1.6 to 3.501, inclusive.

For more information about Float see Ruby's Float class docs.

The Numeric data type

The data type of all numbers, both integer and floating point, is Numeric. It matches any integer or floating point number, and takes no parameters.

Numeric is equivalent to Variant[Integer, Float]. If you need to set size limits but still accept both integers and floats, you can use the abstract type Variant to construct an appropriate data type. For example:
Variant[Integer[-3,3], Float[-3.0,3.0]]