Skip to content

unicorn/prefer-math-trunc Pedantic

🚧 An auto-fix is still under development.

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.

Example

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 in the CLI or using the config file, you can use:

bash
oxlint --deny unicorn/prefer-math-trunc
json
{
  "rules": {
    "unicorn/prefer-math-trunc": "error"
  }
}

References

Released under the MIT License.