Perl Numbers

Summary: in this tutorial, you’ll learn about two kinds of Perl numbers: integer and floating-point numbers.

Perl integers

Integers are whole numbers that have no digits after the decimal points i.e 10-20 or 100.

In Perl, integers are often expressed as decimal integers, base 10. The following illustrates some integer numbers:

#!/usr/bin/perl use warnings; use strict; $x = 20; $y = 100; $z = -200;
Code language: Perl (perl)

When the integer number is big, you often use a comma as a separator to make it easier to read e.g., 123,763,213.

However, Perl already uses a comma (,) as a separator in the list so for integer numbers Perl uses underscore character ( _) instead. In this case,  123,763,213 is written in Perl as 123_763_213.

Take a look at the following example:

my $a = 123_763_213; print($a, "\n"); # 123763213
Code language: Perl (perl)

In the output of the example above, Perl uses no comma or underscore as the separator.

Besides the decimal format, Perl also supports other formats including binary, octal and hexadecimal.

The following table shows you prefixes for formatting with binary, octal and hexadecimal integers.

NumberFormat
0b123Binary integer using a prefix of 0b
0255Octal integer using prefix of 0
0xABCHexadecimal integer using a prefix of 0x

All the following integer numbers are 12 in Perl:

12 0b1100 014 0xC
Code language: Perl (perl)

Perl floating-point numbers

Perl Numbers

You use floating-point numbers to store real numbers. Perl represents floating-point numbers in two forms:

  • Fixed point: decimal point is fixed in the number to denote fractional part starts e.g., 100.25
  • Scientific: consists of a significand with the actual number value and an exponent represent the power of 10 that the significand is multiplied by, for example,   +1.0025e2 or  -1.0025E2 is 100.25.

Floating-point number holds 8 bytes, with 11 bits reserved for exponent and 53 bits for significand.

The range of floating-point numbers is essentially determined by the standard C library of the underlying platform where Perl is running.

In this tutorial, you have learned two kinds of Perl numbers including integer and floating-point numbers.

Was this tutorial helpful ?