--- name: int signature: 'int EXPR' since: 5.0 status: documented categories: ["Numeric functions"] --- ```{index} single: int; Perl built-in ``` *[Numeric functions](../perlfunc-by-category)* # 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 [`$_`](../perlvar). ## Synopsis ```perl 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. ```perl 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: ```perl 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: ```perl print int(-3.9), "\n"; # -3 (not -4) print int(-3.1), "\n"; # -3 ``` Works on [`$_`](../perlvar) when called with no arguments — handy inside [`map`](map) or `while (<>)`: ```perl my @whole = map { int } @floats; # truncate every element ``` Strings are coerced, keeping the leading numeric prefix: ```perl 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**: ```perl 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`: ```perl 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 - **`int` on [`$_`](../perlvar)**: with no argument, operates on [`$_`](../perlvar). Inside `map { int }` this is what you want; inside a larger expression it can surprise a reader who missed the implicit argument. Prefer `int($x)` when the source variable isn't obvious from context. - **Already-integer input**: `int(5)` is `5`. No conversion, no warning, no precision loss. - **Very large floats lose precision before `int` sees them**. A double-precision float can represent every integer up to `2**53` exactly; beyond that, consecutive representable values are spaced `2`, then `4`, then `8` apart. `int(1e20)` returns a float whose integer value is close to but not equal to `10**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." Use `sprintf "%.0f"` or `POSIX::floor`/`POSIX::ceil` if you need rounding behaviour that tolerates this. - **Infinities and NaN**: `int('Inf')` returns `Inf`; `int('-Inf')` returns `-Inf`; `int('NaN')` returns `NaN`. `int` has 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`](undef)**: `int(undef)` returns `0` and emits a `Use of uninitialized value` warning under `use warnings`. - **Non-numeric strings**: `int("abc")` returns `0` and emits `Argument "abc" isn't numeric` under `use warnings`. A string that starts with a numeric prefix (`"42abc"`) yields the prefix's integer value (`42`) with the same warning — `int` inherits this from Perl's standard numeric coercion, not from any custom parsing. - **Booleans and dualvars**: `int` reads the numeric half. `int(!!1)` is `1`; `int(!!0)` is `0`. For a dualvar whose string and numeric values disagree, `int` uses the numeric side. ## Differences from upstream Fully compatible with upstream Perl 5.42. ## See also - [`abs`](abs) — magnitude without sign; pair with `int` when you want the truncated magnitude regardless of sign - [`sprintf`](sprintf) — `sprintf "%d", $x` also truncates toward zero, but additionally formats the result as a string; use when you want the truncated value *and* a specific textual form - [`POSIX::floor`](../../POSIX) — round *toward negative infinity*; `floor(-3.9)` is `-4`, which is what most mathematical contexts (graphics, bucketing) actually want - [`POSIX::ceil`](../../POSIX) — round *toward positive infinity*; `ceil(-3.1)` is `-3`, `ceil(3.1)` is `4`; the mirror of `floor` - [`POSIX::round`](../../POSIX) — round to the nearest integer, halves away from zero; use this when you want statistical rounding rather than truncation - [`sprintf`](sprintf) with `"%.0f"` — round to nearest integer, halves to even (banker's rounding) on most platforms; subtly different from `POSIX::round` at half-values