What is the int() function in Perl?

Overview

The int() function is used on a number like a decimal number. It returns the integer part. For example, 15.4 will be returned as 15 when the int() function is invoked on the number value.

Note: The int() function does not do any rounding.

Syntax

int(value)

Parameters

value: This is the value we want to convert into an integer.

Return Value

The value returned is the integer part of value.

Code example

# create some numeric values
$val1 = 12.34;
$val2 = 3/2;
$val3 = -2.555;
# get int values
$int1 = int($val1);
$int2 = int($val2);
$int3 = int($val3);
# print results
print "$val1 is $int1\n";
print "$val2 is $int2\n";
print "$val3 is $int3";

Explanation

  • Lines 2 to 4: We create some values.
  • Lines 7 to 9: We get the integer values of our created values.
  • Lines 12 to 14: The results are printed to the console.

Free Resources