1. Perl
  2. Syntax
  3. here

Numeric literals - Representation of numbers in source code

The representation of numbers in the source code is called number literals . Numeric literals can be written as follows.

integer

Numbers that are not enclosed in quotes or double quotes are interpreted by Perl as numeric literals.

my $num1 = 1;

# Negative number
my $num2 = -1;

Digit separator expression

If an underscore is included in a numeric literal, it will be ignored. The notations 1_000_000 and 1000000 are equivalent. Underscores can be used to make large numbers easier to read.

# You can use _ as a digit separator.
my $num3 = 1_000_000;

Decimal (floating point)

If it contains a. (Dot), it is interpreted as a decimal. It is the same as the notation in mathematics. A few Perls are double precision floating point.

# Decimal point
my $num4 = 1.33;

Minority exponential notation

You can use exponential notation to represent a small number of numbers. 1.3E3 is equivalent to 1300. 1.3E-3 is equivalent to 0.0013.

# 1.3 x 10 cubed
my $num5 = 1.3E3;

# 1.3 x 10 to the -3rd power
my $num6 = 1.3E-3;

Binary number

You can specify a numerical value in binary by prefixing it with 0b.

my $num_bin = 0b1111;

Octal number

If you add 0 to the beginning, you can specify a numerical value in octal.

my $num_oct = 0777;

Hexadecimal

You can specify a number in hexadecimal by prefixing it with 0x.

my $num_hex = 0xFFFF;

What are the numbers in Perl?

The numbers in Perl are explained in detail below.

Example program

This is an example program that uses numerical literals.

use strict;
use warnings;

my $num1 = 1;

# Negative number
my $num2 = -1;

# You can use _ as a digit separator.
my $num3 = 1_000_000;

# Decimal
my $num4 = 1.33;

# Minority exponential notation 1.3 x 10 cubed
my $num5 = 1.3E3;

# 1.3 x 10 to the -3rd power
my $num6 = 1.3E-3;

print "\$num1 = $num1\n";
print "\$num2 = $num2\n";
print "\$num3 = $num3\n";
print "\$num4 = $num4\n";
print "\$num5 = $num5\n";
print "\$num6 = $num6\n";

Related Informatrion