Skip to content
← Back to rules

unicorn/prefer-math-trunc Pedantic

💡 A suggestion is available for this rule.

What it does ​

Prefers use of Math.trunc() instead of bitwise operations for clarity and more reliable results.

It prevents the use of the following bitwise operations:

Why is this bad? ​

Using bitwise operations to truncate numbers is not clear and do not work in some cases.

Examples ​

Examples of incorrect code for this rule:

javascript
const foo = 1.1 | 0;

Examples of correct code for this rule:

javascript
const foo = Math.trunc(1.1);

How to use ​

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "unicorn/prefer-math-trunc": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "unicorn/prefer-math-trunc": "error",
  },
});
bash
oxlint --deny unicorn/prefer-math-trunc

Version ​

This rule was added in v0.0.18.

References ​