📜  珀尔 | int()函数

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

珀尔 | int()函数

Perl 中的 int()函数返回给定值的整数部分。如果未提供任何值,则返回 $_。请注意, $_ 是默认输入,在这种情况下为 0。 int()函数不进行舍入。要将值四舍五入为整数,请使用 sprintf。

示例 1:

#!/usr/bin/perl
  
# Passing positive decimal value
$int_val = int(19.8547);
print"Integer value is $int_val\n";
  
# Passing negative decimal value
$int_val = int(-18.659);
print"Integer value is $int_val\n";

输出:

Integer value is 19
Integer value is -18

示例 2:

#!/usr/bin/perl
  
# Passing fractional positive value
$int_val = int(17 / 4);
print"Integer value is $int_val\n";
  
# Passing fractional negative value
$int_val = int(-17 / 4);
print"Integer value is $int_val\n";

输出:

Integer value is 4
Integer value is -4