big-amount

BigInt-based rational number library focused on accounting
- Stores amount as ratio of two BigInts, allowing precise calculation of
arbitrarily large rational numbers
- Optimized for addition of fractions that share same denominator; little
overhead on aggregating many cents (1/100)
- Supports rounding and formatting as decimal
import { q, BigAmount } from "big-amount";
let f = q("1/2")
.neg()
.inv()
.add(q("34.5"))
.sub(q(".67"))
.mul(q(-8n, 9n))
.div(q(10))
.abs()
.reduce();
console.log(f.toJSON());
console.log(f.toFixed(6));
let s = BigAmount.sum([
"2200811.81",
"5954398.62",
"-6217732.25",
"-9336803.50",
]).toFixed(2, {
groupSeparator: ",",
templates: ["${}", "$({})"],
});
console.log(s);
Note that this library requires ES2020 compatibility (as does BigInt).
Instance Creation
q
and BigAmount.create
Use BigAmount.create or the equivalent shortcut q
to create an instance.
BigAmount.create(x)
creates an instance representing x/1.
q(123n);
q(123);
q("123");
q("123.45");
q("123.45e-6");
q("123/45");
q("12.3/-4.5");
BigAmount.create(x, y)
creates an instance representing x/y.
q(123n, 45n);
q(123, 45);
q(123, 45n);
q(123n, "4.5");
q("1/2", "3/4");
q("1/2", "3.4");
Note that non-integral number
values have to be passed as string
.
q(123.45);
q(123 / 45);
BigAmount.fromNumber
Use BigAmount.fromNumber to create an instance from a non-integral number
value.
BigAmount.fromNumber(Math.PI);
Arithmetic Operations
neg() | Negation (unary - ) |
add(other) | Addition (this + other ) |
sub(other) | Subtraction(this - other ) |
mul(other) | Multiplication (this * other ) |
div(other) | Division (this / other ) |
inv() | Inverse (1 / this ) |
abs() | To absolute value |
reduce() | To irreducible form |
reduce()
needs to be called explicitly when an irreducible fraction is
necessary, because the arithmetic operations do not return the canonical form of
a resulting fraction. It is currently guaranteed that add()
and sub()
keep
the denominator unchanged if both operands share the same denominator; in all
other cases, the returned object will have implementation-dependent numerator
and denominator.
The following methods are also available for convenience and optimization to
handle accounting use cases.
batchAdd(others) | Adds array of BigAmount values at a time |
fixedAdd(other) | Addition that keeps the denominator unchanged |
fixedSub(other) | Subtraction that keeps the denominator unchanged |
fixedMul(other) | Multiplication that keeps the denominator unchanged |
fixedDiv(other) | Division that keeps the denominator unchanged |
quantMul(other, den) | Multiplication that resets the denominator to den |
quantDiv(other, den) | Division that resets the denominator to den |
Rounding
round()
rounds a fraction at a decimal place like the round
functions of
Python and Excel.
q("123.456").round(2);
q("123.456").round(-1);
Use quantize()
to convert a fraction to another that has the specified
denominator.
q("123/4").quantize(2n);
The rounding functions round ties to the nearest even numbers (i.e. bankers'
rounding) by default. See RoundingMode for rounding mode options.
Conversion to String
toJSON()
generates the preferred form to seriarize a rational number.
JSON.stringify(q("12.345"));
Use toFixed()
to format a fraction as decimal. This method takes some
formatting options to customize the output.
let f = BigAmount.create("123456789/10");
f.toFixed(2);
f.toFixed(2, { decimalSeparator: "," });
f.toFixed(2, { groupSeparator: "," });
f.neg().toFixed(2, {
decimalSeparator: ",",
groupSeparator: " ",
templates: ["{} €"],
});
const opts = { templates: ["${}", "$({})", "-"] };
BigAmount.create("123.45").toFixed(2, opts);
BigAmount.create("-678.9").toFixed(2, opts);
BigAmount.create("0").toFixed(2, opts);
toExponential()
returns a string representing a fraction in exponential
notation.
q("12345.678").toExponential(6);
License
Licensed under the Apache License, Version 2.0.
See Also