https://github.com/writetome51/get-rounded-to-precision
https://github.com/writetome51/get-rounded-to-precision
javascript precision round typescript
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/writetome51/get-rounded-to-precision
- Owner: writetome51
- License: mit
- Created: 2018-10-12T22:14:06.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2020-09-16T18:39:47.000Z (almost 6 years ago)
- Last Synced: 2025-07-03T03:05:29.801Z (12 months ago)
- Topics: javascript, precision, round, typescript
- Language: TypeScript
- Size: 19.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# getRoundedToPrecision(
num,
positionRelativeToDecimal: integer
): number
Returns `num` rounded to `positionRelativeToDecimal`.
To round to a position on the left of the decimal, set it to a negative number.
To round to a position on the right of the decimal, set it to a positive number.
Its algorithm prevents cumulative rounding errors.
## Examples
```js
getRoundedToPrecision(123.1, -2); // --> 100
getRoundedToPrecision(123.1, -1); // --> 120
getRoundedToPrecision(123.1, 0); // --> 123
getRoundedToPrecision(123.199, 2); // --> 123.2
getRoundedToPrecision(123.19955, 4); // --> 123.1996
getRoundedToPrecision(1.8545, 3); // --> 1.854
// if `num` is integer, it returns `num` unchanged.
getRoundedToPrecision(1, 2); // --> 1
```
## Installation
`npm i @writetome51/get-rounded-to-precision`
## Loading
```js
import {getRoundedToPrecision} from '@writetome51/get-rounded-to-precision';
```