📜  珀尔 | abs()函数

📅  最后修改于: 2022-05-13 01:54:52.338000             🧑  作者: Mango

珀尔 | abs()函数

此函数返回其参数的绝对值。如果传递了一个纯整数值,那么它将按原样返回,但如果传递一个字符串,那么它将返回零。如果 VALUE 被省略,那么它使用 $_

示例 1:

#!/usr/bin/perl
  
# Defining decimal value
$var1 = 15.8;
  
# Defining integer value
$var2 = 7;
  
# Defining negative value
$var3 = "-15.2";
  
# Calling abs() function
$res1 = abs($var1);
$res2 = abs($var2);
$res3 = abs($var3);
  
# Printing these values
print "Abs value of var1 is $res1\n";
print "Abs value of var2 is $res2\n";
print "Abs value of var3 is $res3";

输出:

Abs value of var1 is 15.8
Abs value of var2 is 7
Abs value of var3 is 15.2

示例 2:

#!/usr/bin/perl
  
# Defining string value
$var1 = "Geeks";
  
# Defining Fractional value
$var2 = 7/2;
  
# Calling abs() function
$res1 = abs($var1);
$res2 = abs($var2);
  
# Print these values
print "Abs value of var1 is $res1\n";
print "Abs value of var2 is $res2";

输出:

Abs value of var1 is 0
Abs value of var2 is 3.5