int#
Return the integer portion of a number, truncating toward zero.
int takes a single scalar, forces it to numeric context, and drops
its fractional part. The discard is always toward zero — not
toward negative infinity — so int(3.9) is 3 and int(-3.9) is
-3, not -4. Non-numeric strings are coerced with the usual Perl
numeric-conversion rules (leading whitespace, optional sign, digits;
the rest is discarded, with an Argument "..." isn't numeric warning
under use warnings). If EXPR is omitted, int operates on $_.
Synopsis#
int EXPR
int
int($x)
What you get back#
A number with no fractional part, same sign as the input. The result is an integer value if the truncated magnitude fits in the platform’s native integer range (typically 64-bit); otherwise Perl keeps it as a floating-point value whose fractional part happens to be zero. Either way it compares and prints as an integer.
my $n = int(7.8); # 7
my $m = int(-7.8); # -7
int is not a rounding function. For rounding, see See also.
Examples#
Positive truncation — the fractional part is discarded:
print int(3.9), "\n"; # 3
print int(3.1), "\n"; # 3
print int(3.0), "\n"; # 3
Negative truncation — the trap. int truncates toward zero, so
int(-3.9) is -3, which is larger than -3.9. This is the
opposite of a mathematical floor:
print int(-3.9), "\n"; # -3 (not -4)
print int(-3.1), "\n"; # -3
Works on $_ when called with no arguments — handy inside map or
while (<>):
my @whole = map { int } @floats; # truncate every element
Strings are coerced, keeping the leading numeric prefix:
print int("42.7 bottles"), "\n"; # 42
print int(" -5.5xyz"), "\n"; # -5
print int("hello"), "\n"; # 0 (with a warning under `use warnings`)
Division producing a fraction, then truncated — a common idiom for integer division of signed values, with the caveat above about negatives:
my $pages = int($total_items / $per_page); # floor only for non-negative $total_items
The classic floating-point surprise. -6.725 / 0.025 is mathematically
-269, but the IEEE-754 result is slightly greater than -269 (around
-268.99999999999994). int truncates toward zero, giving -268:
print int(-6.725 / 0.025), "\n"; # -268, not -269
This is not an int bug — every truncation/rounding function has to
cope with inputs like this. See Edge cases below.
Edge cases#
inton$_: with no argument, operates on$_. Insidemap { int }this is what you want; inside a larger expression it can surprise a reader who missed the implicit argument. Preferint($x)when the source variable isn’t obvious from context.Already-integer input:
int(5)is5. No conversion, no warning, no precision loss.Very large floats lose precision before
intsees them. A double-precision float can represent every integer up to2**53exactly; beyond that, consecutive representable values are spaced2, then4, then8apart.int(1e20)returns a float whose integer value is close to but not equal to10**20. If you need exact large integers, keep them as integers throughout — don’t route them through floating point.Floating-point representation artefacts: inputs like
-6.725 / 0.025(see Examples) don’t land on the mathematical integer they “should.” Usesprintf "%.0f"orPOSIX::floor/POSIX::ceilif you need rounding behaviour that tolerates this.Infinities and NaN:
int('Inf')returnsInf;int('-Inf')returns-Inf;int('NaN')returnsNaN.inthas no integer to truncate to, so it returns the special value unchanged. This matches every alternative (sprintf "%d",POSIX::floor,POSIX::ceil) — none of them give you a finite integer here.undef:int(undef)returns0and emits aUse of uninitialized valuewarning underuse warnings.Non-numeric strings:
int("abc")returns0and emitsArgument "abc" isn't numericunderuse warnings. A string that starts with a numeric prefix ("42abc") yields the prefix’s integer value (42) with the same warning —intinherits this from Perl’s standard numeric coercion, not from any custom parsing.Booleans and dualvars:
intreads the numeric half.int(!!1)is1;int(!!0)is0. For a dualvar whose string and numeric values disagree,intuses the numeric side.
Differences from upstream#
Fully compatible with upstream Perl 5.42.
See also#
abs— magnitude without sign; pair withintwhen you want the truncated magnitude regardless of signsprintf—sprintf "%d", $xalso truncates toward zero, but additionally formats the result as a string; use when you want the truncated value and a specific textual formPOSIX::floor— round toward negative infinity;floor(-3.9)is-4, which is what most mathematical contexts (graphics, bucketing) actually wantPOSIX::ceil— round toward positive infinity;ceil(-3.1)is-3,ceil(3.1)is4; the mirror offloorPOSIX::round— round to the nearest integer, halves away from zero; use this when you want statistical rounding rather than truncationsprintfwith"%.0f"— round to nearest integer, halves to even (banker’s rounding) on most platforms; subtly different fromPOSIX::roundat half-values