Open In App

Perl | Number and its Types

Last Updated : 22 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  • Integers: Perl integers are represented as decimal number with base 10. These numbers can be positive and negative both. However, Perl uses “_” to represent big integer numbers to increase readability.

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

Perl




#!/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
  • Floating Numbers: Floating point numbers in Perl are those which contain integer as well as fractional values separated by a decimal point. The integer part of a Floating Number could be positive or negative both but the fractional value can only be a positive value. Floating Numbers in Perl can be represented in two ways:
    • Fixed Point: In this representation decimal point is fixed. This decimal point denotes the starting of the fractional part.

Example: 12.5678

  • Scientific: This representation consists of two parts, significand which represents the actual number and exponent which represents the power of 10 with which the significand is multiplied.

Example: 1.23567e-3 represents 0.00123567

Perl




#!/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
  • Hexadecimal Numbers Hexadecimal numbers are numbers with base 16 i.e. they range from 0-15 represented as 0xa with ‘0x’ preceding the number whereas ‘a’ here is the value of 10 in Hex form. These alphabets range from ‘a’ to ‘f’ representing 10 to 15. Hexadecimal numbers can be both negative and positive.

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

Perl




#!/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.

  • Octal Numbers Octal numbers are the numbers with base 8 i.e. they range from 0-7. These numbers are represented as 057 with ‘0’ preceding the number and the rest part is the octal value of the required decimal number. Just like other types, octal numbers can also be negative as well as positive.

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

Perl




#!/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

  • Binary Numbers Binary numbers are number with base 2 i.e. they are formed of only two values 0 and 1. These numbers are represented as 0b1010 with ‘0b’ preceding the number. Example: For binary number ‘0b1010′ the decimal equivalent will be ’10’. 

Perl




#!/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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads