JSPM

@writetome51/get-rounded-to-precision

3.0.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1
  • Score
    100M100P100Q66536F
  • License MIT

Rounds number to specific number of digits before or after decimal

Package Exports

  • @writetome51/get-rounded-to-precision

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@writetome51/get-rounded-to-precision) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

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

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

import {getRoundedToPrecision} from '@writetome51/get-rounded-to-precision';