Skip to content

useMathTrunc (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"useMathTrunc": "error"
}
}
}
}

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) is 0. They also turn NaN into 0.
  • 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 reports these patterns, where the bitwise operations use the number 0:

  • x | 0, x ^ 0, x << 0, and x >> 0
  • x |= 0, x ^= 0, x <<= 0, and x >>= 0
  • ~~x
  • parseInt(String(x), 10) and Number.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.

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);