- addition (
- subtraction (
- multiplication (
- division (
- truncate (
- floor (
- ceiling (
- absolute value (
This package is built on top of the excellent
bignumber.js package for arbitrary-precision arithmetic. It is written in TypeScript. Unit tests are in Jest.
This package aims to work with only normalized values in the Decimal128 universe. With this package, there is no way to represent, say,
1.20 as distinct values. Digit strings are normalized right away, so
Differences with the official Decimal128
This package is not literally an implementation of Decimal128. In time, it may become one, but initially, this package is working with a subspace of Decimal128 that makes sense for the use cases we have in mind (mainly, finance).
Lack of support for specifying context
Decimal128 allows one to specify, globally, some configuration values (such as precision) that control all mathematical operations on Decimal128 values.
Think of this package as providing, basically, arbitrary-precision decimal numbers limited to those that fit into 128 bits the way that Decimal128 does it. No need to specify context. Just imagine that you're working in an ideal arbitrary-precision world, do the operation, and enjoy the results. (Of course, this can't always work. But this suggestion is being offered to help you clear you mind of any "arithmetic context" cobwebs, if you have any.)
Values always normalized
Decimal128 is a universe of unnormalized values. In the Decimal128 world,
1.20 are distinct values. There's good reason for adopting such an approach, and some benefits. But this package deliberately works in a world of normalized values. Given the string
1.20, this package will turn that into
1.2; that extra trailing zero will be lost. To recover the string
1.20, additional, out-of-band information needs to be supplied. For instance: if you're working with numbers as financial quantities, you know, out-of-band, how to interpret your numbers. Thus, if I tell you that the cost of something is
1.2 USD, you know that means, and you know that, if you need to present that data to someone, you'd add an extra digit there. This package provides a
toDecimalDigits method that allows you to generate
1.20 from the underlying
This package focuses on the bread and butter of arithmetic: addition, multiplication, subtraction, and division. To round things out from there (ha!), we have the absolute value function, trunction, floor/ceiling.
In-scope but not yet implemented
- Exponential notation (e.g.,
- Options for operations, such as cutting off calculation after a certain number of digits
Potentially in-scope, but currently unimplemented
- A variant for unnormalized Decimal128 values. Decimal128 values are, out-of-the-box, not normalized.