Round a number to a specific number of decimal places: 1.234 → 1.2
JavaScript
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.editorconfig Require Node.js 6 Apr 12, 2018
.gitattributes ES2015ify and require Node.js 4 Jan 23, 2017
.gitignore Require Node.js 6 Apr 12, 2018
.npmrc Require Node.js 6 Apr 12, 2018
.travis.yml Require Node.js 6 Apr 12, 2018
index.js Require Node.js 6 Apr 12, 2018
license Require Node.js 6 Apr 12, 2018
package.json 3.0.0 Apr 12, 2018
readme.md Require Node.js 6 Apr 12, 2018
test.js Fix NaN issue Apr 12, 2018

readme.md

round-to Build Status

Round a number to a specific number of decimal places: 1.2341.2

Install

$ npm install round-to

Usage

const roundTo = require('round-to');

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

roundTo.up(1.234, 2);
//=> 1.24

roundTo.down(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.

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

API

roundTo(value, precision)

Round the decimals with Math.round.

roundTo.up(value, precision)

Round up the decimals with Math.ceil.

roundTo.down(value, precision)

Round down the decimals with Math.floor.

value

Type: number

Number to adjust.

precision

Type: number (integer)

Number of decimal places.

License

MIT © Sindre Sorhus