Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sindresorhus/round-to
Round a number to a specific number of decimal places: 1.234 → 1.2
https://github.com/sindresorhus/round-to
Last synced: 5 days ago
JSON representation
Round a number to a specific number of decimal places: 1.234 → 1.2
- Host: GitHub
- URL: https://github.com/sindresorhus/round-to
- Owner: sindresorhus
- License: mit
- Created: 2015-08-13T18:37:18.000Z (over 9 years ago)
- Default Branch: main
- Last Pushed: 2023-08-05T10:34:53.000Z (over 1 year ago)
- Last Synced: 2025-02-02T17:09:11.948Z (12 days ago)
- Language: JavaScript
- Size: 24.4 KB
- Stars: 155
- Watchers: 9
- Forks: 11
- Open Issues: 2
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
- awesome-nodejs-cn - round-to - 将数字四舍五入到指定的小数位数:```1.234``` → ```1.2``` (包 / 数字)
- awesome-nodejs - round-to - Round a number to a specific number of decimal places: `1.234` → `1.2`. ![](https://img.shields.io/github/stars/sindresorhus/round-to.svg?style=social&label=Star) (Repository / Number)
- awesome-nodejs-cn - round-to - **star:155** 将一个数字四舍五入到一个特定的小数位数:' 1.234 '→' 1.2 ' (包 / 数量)
- stars - sindresorhus/round-to - Round a number to a specific number of decimal places: 1.234 → 1.2 (JavaScript)
- awesome-nodejs - round-to - Round a number to a specific number of decimal places: `1.234` → `1.2`. (Packages / Number)
- awesome-nodejs - round-to - Round a number to a specific number of decimal places: 1.234 → 1.2 - ★ 91 (Number)
- awesome-node - round-to - Round a number to a specific number of decimal places: `1.234` → `1.2`. (Packages / Number)
- awesome-nodejs-cn - round-to - 将数字四舍五入到指定的小数位数:`1.234`→1.2`. (目录 / 数字)
- awesome-frontend - round-to - Round a number to a specific number of decimal places: `1.234` → `1.2`. ![](https://img.shields.io/github/stars/sindresorhus/round-to.svg?style=social&label=Star) (Repository / Number)
README
# round-to
> Round a number to a specific number of decimal places: `1.234` → `1.2`
## Install
```sh
npm install round-to
```## Usage
```js
import {roundTo, roundToUp, roundToDown} from 'round-to';roundTo(1.234, 2);
//=> 1.23roundToUp(1.234, 2);
//=> 1.24roundToDown(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)
Round the decimals with [`Math.round`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round).
### 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.