Perl | abs() function
Last Updated :
25 Jun, 2019
This function returns the absolute value of its argument. If a pure integer value is passed then it will return it as it is, but if a string is passed then it will return zero. If VALUE is omitted then it uses $_
Syntax: abs(VALUE)
Parameter:
VALUE: It is a required number which can be either positive or negative or a string.
Returns: Function returns the absolute value of the arguments passed.
Example 1:
$var1 = 15.8;
$var2 = 7;
$var3 = "-15.2" ;
$res1 = abs ( $var1 );
$res2 = abs ( $var2 );
$res3 = abs ( $var3 );
print "Abs value of var1 is $res1\n" ;
print "Abs value of var2 is $res2\n" ;
print "Abs value of var3 is $res3" ;
|
Output:
Abs value of var1 is 15.8
Abs value of var2 is 7
Abs value of var3 is 15.2
Example 2:
$var1 = "Geeks" ;
$var2 = 7/2;
$res1 = abs ( $var1 );
$res2 = abs ( $var2 );
print "Abs value of var1 is $res1\n" ;
print "Abs value of var2 is $res2" ;
|
Output:
Abs value of var1 is 0
Abs value of var2 is 3.5
Share your thoughts in the comments
Please Login to comment...