# prefer-math-trunc

Rule `unicorn/prefer-math-trunc`. Prefer `Math.trunc()` for truncating numbers.

- Source: unicorn
- Technologies: JavaScript
- Kind: suspicious
- Needs type information: no
- Autofix: yes, in at least one linter
- Reach: 40.5M weekly downloads of presets that enable it
- Trend: +81.2% last quarter
- Rank: #425 by reach, #417 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `unicorn/prefer-math-trunc` | no | suggestion | package `eslint-plugin-unicorn` | [docs](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/v80.0.0/docs/rules/prefer-math-trunc.md) |
| Oxlint | `unicorn/prefer-math-trunc` | no | safe | plugin `unicorn`, category pedantic | [docs](https://oxc.rs/docs/guide/usage/linter/rules/unicorn/prefer-math-trunc.html) |
| Biome | `nursery/useMathTrunc` | yes | unsafe | group nursery, since 2.6.3 | [docs](https://biomejs.dev/linter/rules/use-math-trunc/) |

## Code examples

### Incorrect: the rule reports this

```js
console.log(foo | 0);
```

```js
console.log(~~foo);
```

```js
console.log(foo << 0);
```

### Correct: the rule accepts this

```js
console.log(Math.trunc(foo));
```

```js
foo = Math.trunc(foo);
```

```js
const foo = 37.4;
console.log(foo | 3);
```

Examples from the ESLint documentation.

## Enable it

### ESLint: `eslint.config.js`

```js
import unicorn from "eslint-plugin-unicorn";

export default [
  {
    plugins: { unicorn },
    rules: {
      "unicorn/prefer-math-trunc": "error",
    },
  },
];
```

### Oxlint: `.oxlintrc.json`

```json
{
  "rules": {
    "unicorn/prefer-math-trunc": "error"
  }
}
```

### Biome: `biome.json`

```json
{
  "linter": {
    "rules": {
      "nursery": {
        "useMathTrunc": "error"
      }
    }
  }
}
```

## Presets that enable it

| Preset | Linter | Package | Rules | Weekly downloads | Trend |
| --- | --- | --- | --- | --- | --- |
| [Biome recommended](https://lintradar.dev/presets/biome-recommended.md) | Biome | `@biomejs/biome` | 491 | 28.5M | +106.8% |
| [Unicorn recommended](https://lintradar.dev/presets/unicorn-recommended.md) | ESLint | `eslint-plugin-unicorn` | 340 | 9.5M | +30.3% |
| [Unicorn unopinionated](https://lintradar.dev/presets/unicorn-unopinionated.md) | ESLint | `eslint-plugin-unicorn` | 236 | 9.5M | +30.3% |
| [Ultracite for Biome](https://lintradar.dev/presets/ultracite-biome.md) | Biome | `ultracite` | 616 | 2.1M | +151.7% |
| [Ultracite for Oxlint](https://lintradar.dev/presets/ultracite-oxlint.md) | Oxlint | `ultracite` | 475 | 2.1M | +151.7% |
| [XO](https://lintradar.dev/presets/xo.md) | ESLint | `eslint-config-xo` | 618 | 413K | −5.0% |

## Related rules

- [unicorn/prefer-math-min-max](https://lintradar.dev/rules/unicorn/prefer-math-min-max.md): Prefer `Math.min()` and `Math.max()` over ternaries for simple comparisons.
- [unicorn/prefer-modern-math-apis](https://lintradar.dev/rules/unicorn/prefer-modern-math-apis.md): Prefer modern `Math` APIs over legacy patterns.
- [unicorn/prefer-date-now](https://lintradar.dev/rules/unicorn/prefer-date-now.md): Prefer `Date.now()` to get the number of milliseconds since the Unix Epoch.
- [unicorn/prefer-object-from-entries](https://lintradar.dev/rules/unicorn/prefer-object-from-entries.md): Prefer using `Object.fromEntries(…)` to transform a list of key-value pairs into an object.
- [unicorn/prefer-bigint-literals](https://lintradar.dev/rules/unicorn/prefer-bigint-literals.md): Prefer `BigInt` literals over the constructor.
- [unicorn/no-accidental-bitwise-operator](https://lintradar.dev/rules/unicorn/no-accidental-bitwise-operator.md): Disallow bitwise operators where a logical operator was likely intended.

---

Web page: https://lintradar.dev/rules/unicorn/prefer-math-trunc. Data from npm and the linters' documentation, collected Dec 4, 2026. Site guide for agents: https://lintradar.dev/llms.txt
