Summary: in this tutorial, you will learn about two kinds of Perl numbers: integer and floating-point numbers.
Integers are whole numbers that have no number after the decimal points i.e
100. In Perl, integers are often expressed as decimal integers, base 10. The following illustrates some integer numbers:
$x = 20;
$y = 100;
$z = -200;
When the integer number is big, we 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
Take a look at the following example:
my $a = 123_763_213;
print($a, "\n"); # 123763213
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.
|0b123||Binary integer using a prefix of 0b|
|0255||Octal integer using prefix of 0|
|0xABC||Hexadecimal integer using a prefix of 0x|
All the following integer numbers are 12 in Perl:
Perl floating-point 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.,
- 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,
Floating-point number holds 8 bytes, with 11 bits reserved for exponent and 53 bits for significand. The range of floating-point number is essentially determined by the standard C library of the underlying platform where Perl is running.
In this tutorial, we have introduced you to two kinds of Perl numbers including integer and floating-point numbers.