Perl | int() function

int() function in Perl returns the integer part of given value. It returns $_ if no value provided. Note that $_ is default input which is 0 in this case. The int() function does not do rounding. For rounding up a value to an integer, sprintf is used.

Syntax: int(VAR)

Parameters:
VAR: value which is to be converted into integer

Returns: Returns the integer part of VAR

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/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";

chevron_right


Output:

Integer value is 19
Integer value is -18

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/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";

chevron_right


Output:

Integer value is 4
Integer value is -4


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.