Perl Numbers
Number is a scalar type in perl programming. Number are used to perform mathematical operation. In perl programming there are main two types of numbers is generally used numeric value such as integers and floating point numbers. Generally numbers are used by decimal system which is can be form of positive and negative values.
#!/usr/bin/perl
use warnings;
use strict;
#Integer numeric value
my $age = 21;
#Floating point numbers
my $salary = 89333.245;
#Negative integer value
my $lose = -1000 ;
print $age,"\n";
print $salary,"\n";
print $lose,"\n";
21
89333.245
-1000
In this example all three variables such as $age , $salary and $lose are storing the numeric literal values.
Numeric literals
There are 4 ways to representing a number. Such as binary, octal, decimal and hexadecimal. In perl programming all those formats can be used.
Format | Prefix | Example |
---|---|---|
Binary | 0b | 0b1010, 0b11111 |
Octal | 0 | 012, 014 |
Decimal | -10,10 | |
Hexadecimal | 0x | 0xA ,0xB |
#!/usr/bin/perl
use warnings;
use strict;
#assign binary literals
my $binary = 0b1010;
#assign octal literals
my $octal = 012;
#assign decimal literals
my $decimal = 10;
#assign hexadecimal literals
my $hexadecimal = 0xA;
print $binary,"\n"; #10
print $octal,"\n"; #10
print $decimal,"\n"; #10
print $hexadecimal,"\n"; #10
10
10
10
10
Note that result is always in form of a decimal number system. Internally before initialize a value to variable according to prefix value are converted into a decimal.
Formatting Numeric literals
Generally number is separating by comma for separating a long numbers for example (9,48,5,20) is representing a number. But In programming language comma are not a part of number. So we can use (_) underscore to separating long number literals.
#!/usr/bin/perl
use warnings;
use strict;
my $population = 89_69_47_89;
my $salary = 9_48_5_20.87_5_10;
print $population,"\n";
print $salary,"\n";
89694789
948520.8751
Note that number formatting are used to provide readability of programmer internally perl number are not use underscore.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment