A TypeScript/JavaScript module containing reliable custom functions for rounding, ceiling, flooring, truncating and formatting floating point numbers. Works with Deno, Node.js, Bun, and browsers.
Keywords: rounding, floating-point, precision, math, decimal, ceiling, floor, truncate, typescript, javascript, deno, node, bun
Basic Problem: JS Floating Point Math is highly inaccurate. Found this thread: https://stackoverflow.com/questions/11832914/how-to-round-to-at-most-2-decimal-places-if-necessary
First best solution: Use a library with correct implementation of floating point math.
-
mathjs (CAUTION: not math.js!): https://www.npmjs.com/package/mathjs
- is basically complete, but is FAT: 732 kB, 188 kB minified
- see https://bundlephobia.com/package/[email protected]
-
decimal.js: https://www.npmjs.com/package/decimal.js
- still 31.1 kB, 12.3kB minified
- good: last Publish in 2022 (today: 2024-09-09)
-
currency.js: https://www.npmjs.com/package/currency.js
- 2.2kB, 1kB minified
- no dependencies
- BUT: only compatible with TS allowSyntheticDefaultImports: true + default import
Second best solution: Use a custom implementation of roundToPrecision
@bjesuiter original selected implementation (on 2024-09-09):
https://stackoverflow.com/questions/11832914/how-to-round-to-at-most-2-decimal-places-if-necessary#:~:text=Solution%202%3A%20purely%20mathematical%20(Number.EPSILON)
2025-07-11: refined multiple times
- refined examples and published files
- reworked the whole implementation and added more tests and examples
Initial release
- Update the version in deno.json
- Update all examples & docs
- Run
deno task test
& fix errors - Run
deno task dry
& fix errors` - Commit, make a tag and push the tag (will then be built by github actions)