Open In App

Perl | Number and its Types

A Number in Perl is a mathematical object used to count, measure, and perform various mathematical operations. A notational symbol that represents a number is termed as a numeral. These numerals, in addition to their use in mathematical operations, are also used for ordering(in the form of serial numbers). 

Examples:



1, 2, -4, 5.6, -7.9, 057, 1.67e-10, 0xFFFF

These numbers as per their use can be categorized into various types:

Example: 123,154,454 is represented as 123_154_454






#!/usr/bin/perl
 
# Positive Integer
$x = 20;
 
# Negative Integer
$y = -15;
 
# Big Integer Number
$z = 123_154_454;
 
# Printing these numbers
print("Positive Integer: ", $x, "\n");
 
print("Negative Integer: ", $y, "\n");
 
print("Big Integer: ", $z, "\n");

Output:
Positive Integer: 20
Negative Integer: -15
Big Integer: 123154454

Example: 12.5678

Example: 1.23567e-3 represents 0.00123567




#!/usr/bin/perl
 
# Positive Floating Number
$x = 20.5647;
 
# Negative Floating Number
$y = -15.2451;
 
# Scientific value
$z = 123.5e-10;
 
# Printing these numbers
print("Positive Number: ", $x, "\n");
 
print("Negative Number: ", $y, "\n");
 
print("Scientific Number: ", $z, "\n");

Output:

Positive Number: 20.5647
Negative Number: -15.2451
Scientific Number: 1.235e-08

Example: 0xe represents 14 in Hex and 0xc represents 12




#!/usr/bin/perl
 
# Positive Hexadecimal Number
$x = 0xc;
 
# Negative Hexadecimal Number
$y = -0xe;
 
# Printing these values
print("Positive Hex Number: ", $x, "\n");
print("Negative Hex Number: ", $y, "\n");
 
# To print Hex value
printf("Value in Hex Format: %x", $x);

Output:
Positive Hex Number: 12
Negative Hex Number: -14
Value in Hex Format: c

Hence, to print value of the number in Hexadecimal format, ‘%x’ is used as shown above.

Example: For octal number 057 the decimal equivalent will be 47.




#!/usr/bin/perl
 
# Positive Octal Number
$x = 074;
 
# Negative Octal Number
$y = -074;
 
print("Positive Octal number: ", $x, "\n");
print("Negative Octal number: ", $y, "\n");
 
# To print value in Octal Form
printf("Value in Octal Form: %o", $x);

Output:
Positive Octal number: 60
Negative Octal number: -60
Value in Octal Form: 74

Here, ‘%o’ is used to print the value in its octal form




#!/usr/bin/perl
  
# Positive Binary Number
$x = 0b1010;
 
# Negative Binary Number
$y = -0b10110;
 
# Printing these values
print("Positive Binary Number: ", $x, "\n");
print("Negative Binary Number: ", $y, "\n");
 
# Printing in unsigned binary form
printf("Value in unsigned Binary Form: %b", $x);

Output:
Positive Binary Number: 10
Negative Binary Number: -22
Value in unsigned Binary Form: 1010

In the above code, ‘%b’ is used to print the number in its actual binary form.


Article Tags :