round-ts
v1.0.1
Published
Correctly round values to specified precision with TypeScript
Downloads
6
Maintainers
Readme
round-ts
Correctly round values to specified precision with TypeScript
Key Features
- TypeScript written
- no dependencies (TypeScript is the only dev-dependency)
- uses the best-known rounding algorithm (solves common problems with incorrect results) and simple implemetation
- 3 methods: ceil, floor and round with the similar behavior to Math.xxx methods
Inspired by the packages:
round-tofixed- good accuracy, no TypeScript, no negative precisionround-precision- bad accuracy, no TypeScript, no negative precisionround- usesround-precisionround10- good accuracy, no TypeScriptround-floor-ceil- good accuracy, no TypeScriptlodash.round- good implementation, but contains many compatibility utils
Install
npm install round-ts [-S]Basic Usage
import { ceil, floor, round } from "round-ts"
ceil(0.444, 2) // 0.45
floor(0.444, 2) // 0.44
round(0.444, 2) // 0.44API
ceil(value: number, precision?: number): number
Returns the smallest number of provided precision greater than or equal to its numeric argument.
Params
- value
numberThe value to be rounded. - precision
number(optional, default=0) A number of significant digits. Must be integer. Can be negative.
Returns number The smallest number of provided precision greater than or equal to the given number.
floor(value: number, precision?: number): number
Returns the greatest number of provided precision less than or equal to its numeric argument.
Params
- value
numberThe value to be rounded. - precision
number(optional, default=0) A number of significant digits. Must be integer. Can be negative.
Returns number The largest number of provided precision less than or equal to the specified number.
round(value: number, precision?: number): number
Returns a supplied numeric expression rounded to the nearest number of provided precision.
Params
- value
numberThe value to be rounded. - precision
number(optional, default=0) A number of significant digits. Must be integer. Can be negative.
Returns number The value of the given number rounded to the nearest number of provided precision.
