Search⌘ K
AI Features

Numbers

Explore how Perl handles numbers including integers, floating-point values, and various numeric formats such as binary, octal, and hexadecimal. Understand numeric literals, the use of underscores for readability, how to check if data is numeric, and handling large numbers with Math::BigInt and Math::BigFloat. Gain insight into Perl's undef value and its behavior in numeric and string contexts.

Perl supports numbers as both integers and floating-point values.

We can represent them with scientific notation as well as in binary, octal, and hexadecimal forms:

Perl
my $integer = 42;
say $integer;
my $float = 0.007;
say $float;
my $sci_float = 1.02e14;
say $sci_float;
my $binary = 0b101010;
say $binary;
my $octal = 052;
say $octal;
my $hex = 0x20;
say $hex;
# only in Perl 5.22 or later
my $hex_float = 0x1.0p-3;
say $hex_float;

Internal representation of different formats

The numeric prefixes 0b, 0, and 0x specify binary, octal, and hex notation. Be aware that a leading zero on an integer always indicates the octal mode.

Note: Even though we can write floating-point values explicitly with perfect accuracy, Perl represents them internally in a binary format, like most programming languages. This representation is sometimes imprecise in ...