Perl abs 函式



描述

此函式返回其引數的絕對值。如果傳遞純粹整數值,則它將按原樣返回,但如果傳遞字串,則它將返回零。如果省略 VALUE,則它使用 $_

語法

此函式的簡單語法如下 -

abs VALUE

abs

返回值

此函式返回其引數的絕對值。

示例

以下是顯示其基本用法的示例程式碼 -

#!/usr/bin/perl

$par1 = 10.25;
$par2 = 20;
$par3 = "ABC";

$abs1 = abs($par1);
$abs2 = abs($par2);
$abs3 = abs($par3);

print "Abs value of par1 is $abs1\n" ;
print "Abs value of par2 is $abs2\n" ;
print "Abs value of par3 is $abs3\n" ;

執行上述程式碼時,會產生以下結果 -

Abs value of par1 is 10.25
Abs value of par2 is 20
Abs value of par3 is 0
perl_function_references.htm
廣告
© . All rights reserved.