# round-to

> Round a number to a specific number of decimal places: `1.234` → `1.2`

Latest version **7.0.0** (published 2025-09-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install round-to
pnpm add round-to
yarn add round-to
bun add round-to
```

## Health

**Score 40/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 7.0.0 |
| Published | 2025-09-17 |
| First published | 2015-08-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | ^12.20.0 \|\| ^14.13.1 \|\| >=16.0.0 |
| Dependencies | 0 |
| Unpacked size | 10.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 159 |
| Author | Sindre Sorhus |
| Maintainers | sindresorhus |
| Keywords | round, number, decimal, places, ceil, floor, math, increment, precision |

## Links

- npm: https://www.npmjs.com/package/round-to
- Repository: https://github.com/sindresorhus/round-to
- Homepage: https://github.com/sindresorhus/round-to#readme
- Issues: https://github.com/sindresorhus/round-to/issues
- Funding: https://github.com/sponsors/sindresorhus
- npm.io page: https://npm.io/package/round-to

## Alternatives

- [random-seedable](https://npm.io/package/random-seedable.md) — 27.9K weekly downloads
- [n2words](https://npm.io/package/n2words.md) — 22.2K weekly downloads
- [@stdlib/math-base-special-factorialln](https://npm.io/package/@stdlib/math-base-special-factorialln.md) — 5.7K weekly downloads
- [@stdlib/math-base-special-abs2](https://npm.io/package/@stdlib/math-base-special-abs2.md) — 1.7K weekly downloads
- [commons-math-interpolation](https://npm.io/package/commons-math-interpolation.md) — 1.4K weekly downloads

## Recent versions

- 7.0.0 (latest) — 2025-09-17
- 6.0.0 — 2021-11-05
- 5.0.0 — 2020-10-20
- 4.1.0 — 2020-01-16
- 4.0.0 — 2019-04-26
- 3.0.0 — 2018-04-12
- 2.0.0 — 2017-01-23
- 1.1.0 — 2015-10-01
- 1.0.0 — 2015-08-13

## README

# round-to

> Round a number to a specific number of decimal places: `1.234` → `1.2`

> [!TIP]
> If you only need a string, use `number.toFixed(precision)`.

## Install

```sh
npm install round-to
```

## Usage

```js
import {roundTo, roundToUp, roundToDown} from 'round-to';

roundTo(1.234, 2);
//=> 1.23

roundToUp(1.234, 2);
//=> 1.24

roundToDown(1.234, 2);
//=> 1.23
```

Numbers are rounded to a specific number of fractional digits. Specifying a negative `precision` will round to any number of places to the left of the decimal.

```js
roundTo(1234.56, -2);
//=> 1200
```

Specifying an infinite `precision` will assume infinite decimal places.

```js
roundTo(0.1231782638, Infinity);
//=> 0.1231782638
```

## API

### roundTo(number, precision, options?)

Round the decimals with [`Math.round`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round) by default.

You can specify different rounding rules using the `roundingRule` option. The default behavior rounds away from zero for 0.5 cases (traditional rounding), but you can use banker's rounding, always round up/down, or other rules.

```js
// Default behavior (away from zero)
roundTo(5.5, 0);
//=> 6
roundTo(-5.5, 0);
//=> -6

// Banker's rounding (to nearest even)
roundTo(5.5, 0, {roundingRule: 'toNearestOrEven'});
//=> 6
roundTo(4.5, 0, {roundingRule: 'toNearestOrEven'});
//=> 4

// Always round up (toward +∞)
roundTo(5.2, 0, {roundingRule: 'up'});
//=> 6
roundTo(-5.2, 0, {roundingRule: 'up'});
//=> -5
```

### roundToUp(number, precision)

Round up the decimals with [`Math.ceil`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/ceil).

### roundToDown(number, precision)

Round down the decimals with [`Math.floor`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor).

#### number

Type: `number`

The number to adjust.

#### precision

Type: `number` *(Integer or Infinity)*

The number of decimal places.

#### options

Type: `object`

##### roundingRule

Type: `'toNearestOrAwayFromZero' | 'toNearestOrEven' | 'up' | 'down' | 'towardZero' | 'awayFromZero'`\
Default: `'toNearestOrAwayFromZero'`

The rounding rule to use:

- `'toNearestOrAwayFromZero'` - Round to the closest value; if two values are equally close, the one with greater magnitude is chosen (traditional rounding).
- `'toNearestOrEven'` - Round to the closest value; if two values are equally close, the even one is chosen (banker's rounding).
- `'up'` - Round toward +∞ (always round up).
- `'down'` - Round toward -∞ (always round down).
- `'towardZero'` - Round toward zero (truncate).
- `'awayFromZero'` - Round away from zero.

Examples:

```js
roundTo(5.5, 0, {roundingRule: 'toNearestOrEven'});
//=> 6
roundTo(4.5, 0, {roundingRule: 'toNearestOrEven'});
//=> 4

roundTo(-5.2, 0, {roundingRule: 'towardZero'});
//=> -5
roundTo(-5.8, 0, {roundingRule: 'towardZero'});
//=> -5
```

---
_Source: https://npm.io/package/round-to · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
