useMathTrunc (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.6.3 - Diagnostic Category:
lint/nursery/useMathTrunc - This rule has an unsafe fix.
- The default severity of this rule is warning.
- Sources:
- Same as
unicorn/prefer-math-trunc
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useMathTrunc": "error" } } }}Description
Section titled “Description”Require Math.trunc() instead of tricks that remove the decimal part of a number.
Some code removes the decimal part of a number with a bitwise operation, such as x | 0 or ~~x,
or by turning the number into a string and parsing it back, as in parseInt(String(x), 10).
These patterns are hard to read, and they give wrong results for some numbers:
- Bitwise operations turn the number into a 32-bit integer first.
Numbers above 2147483647 or below -2147483648 wrap around, so
~~(2 ** 32)is0. They also turnNaNinto0. - Very large and very small numbers turn into strings such as
"1e+21", whichparseInt()reads as1.
Math.trunc()
removes the decimal part of any number and states the intent clearly.
This rule reports these patterns, where the bitwise operations use the number 0:
x | 0,x ^ 0,x << 0, andx >> 0x |= 0,x ^= 0,x <<= 0, andx >>= 0~~xparseInt(String(x), 10)andNumber.parseInt(String(x), 10)
The fix is unsafe because the result can differ from the original code.
Math.trunc() keeps numbers outside the 32-bit range and keeps NaN, unlike bitwise operations.
It also handles values that aren’t numbers differently from parseInt():
parseInt(String("12px"), 10) is 12, while Math.trunc("12px") is NaN.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”const result = value | 0;code-block.js:1:16 lint/nursery/useMathTrunc FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Use Math.trunc() instead of | 0.
> 1 │ const result = value | 0;
│ ^^^^^^^^^
2 │
ℹ Bitwise operations turn the number into a 32-bit integer first, so numbers above 2147483647 or below -2147483648 give wrong results.
ℹ Math.trunc() removes the decimal part of any number and states the intent clearly.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Use Math.trunc() instead.
1 │ - const·result·=·value·|·0;
1 │ + const·result·=·Math.trunc(value);
2 2 │
const result = ~~value;code-block.js:1:16 lint/nursery/useMathTrunc FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Use Math.trunc() instead of \~\~.
> 1 │ const result = \~\~value;
│ ^^^^^^^
2 │
ℹ Bitwise operations turn the number into a 32-bit integer first, so numbers above 2147483647 or below -2147483648 give wrong results.
ℹ Math.trunc() removes the decimal part of any number and states the intent clearly.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Use Math.trunc() instead.
1 │ - const·result·=·\~\~value;
1 │ + const·result·=·Math.trunc(value);
2 2 │
const result = value >> 0;code-block.js:1:16 lint/nursery/useMathTrunc FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Use Math.trunc() instead of >> 0.
> 1 │ const result = value >> 0;
│ ^^^^^^^^^^
2 │
ℹ Bitwise operations turn the number into a 32-bit integer first, so numbers above 2147483647 or below -2147483648 give wrong results.
ℹ Math.trunc() removes the decimal part of any number and states the intent clearly.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Use Math.trunc() instead.
1 │ - const·result·=·value·>>·0;
1 │ + const·result·=·Math.trunc(value);
2 2 │
value |= 0;code-block.js:1:1 lint/nursery/useMathTrunc FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Use Math.trunc() instead of |= 0.
> 1 │ value |= 0;
│ ^^^^^^^^^^
2 │
ℹ Bitwise operations turn the number into a 32-bit integer first, so numbers above 2147483647 or below -2147483648 give wrong results.
ℹ Math.trunc() removes the decimal part of any number and states the intent clearly.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Use Math.trunc() instead.
1 │ - value·|=·0;
1 │ + value·=·Math.trunc(value);
2 2 │
const result = parseInt(String(value), 10);code-block.js:1:16 lint/nursery/useMathTrunc FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Use Math.trunc() instead of parseInt(String(...), 10).
> 1 │ const result = parseInt(String(value), 10);
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Very large and very small numbers turn into strings such as "1e+21", which parseInt() reads as 1.
ℹ Math.trunc() removes the decimal part of any number and states the intent clearly.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Use Math.trunc() instead.
1 │ - const·result·=·parseInt(String(value),·10);
1 │ + const·result·=·Math.trunc(value);
2 2 │
const truncated = Math.trunc(value);const flags = value | 1;const inverted = ~value;const parsed = parseInt(input, 10);See Also
Section titled “See Also”- If you want to disallow bitwise operators entirely, see
noBitwiseOperators.
Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.