npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

big-amount

v2.1.5

Published

BigInt-based rational number library focused on accounting

Downloads

40

Readme

big-amount

npm License

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")           // Same as `BigAmount.create("1/2")`
  .neg()                   // Unary `-`
  .inv()                   // Inverse (`1 / f`)
  .add(q("34.5"))          // `+`
  .sub(q(".67"))           // `-`
  .mul(q(-8n, 9n))         // `*`
  .div(q(10))              // `/`
  .abs()                   // To absolute value
  .reduce();               // To irreducible form

console.log(f.toJSON());   // "1061/375"
console.log(f.toFixed(6)); // "2.829333"

let s = BigAmount.sum([
  "2200811.81",
  "5954398.62",
  "-6217732.25",
  "-9336803.50",
]).toFixed(2, {
  groupSeparator: ",",
  templates: ["${}", "$({})"],
});
console.log(s); // "$(7,399,325.32)"

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);        // 123/1
q(123);         // 123/1
q("123");       // 123/1
q("123.45");    // 12345/100
q("123.45e-6"); // 12345/100000000

q("123/45");    // 123/45
q("12.3/-4.5"); // 1230/-450

BigAmount.create(x, y) creates an instance representing x/y.

q(123n, 45n);    // 123/45
q(123, 45);      // 123/45

q(123, 45n);     // 123/45
q(123n, "4.5");  // 1230/45

q("1/2", "3/4"); // 4/6
q("1/2", "3.4"); // 10/68

Note that non-integral number values have to be passed as string.

q(123.45);   // ERROR!
q(123 / 45); // ERROR!

BigAmount.fromNumber

Use BigAmount.fromNumber to create an instance from a non-integral number value.

BigAmount.fromNumber(Math.PI); // 3.14159...

Arithmetic Operations

| Methods | Operation | | ------------ | ------------------------------- | | 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.

| Method | Operation | | ---------------------- | --------------------------------------------------- | | 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);  // 12346/100
q("123.456").round(-1); // 120/1

Use quantize() to convert a fraction to another that has the specified denominator.

q("123/4").quantize(2n); // 62/2

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")); // '"12345/1000"'

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);                            // "12345678.90"
f.toFixed(2, { decimalSeparator: "," }); // "12345678,90"
f.toFixed(2, { groupSeparator: "," });   // "12,345,678.90"
f.neg().toFixed(2, {
  decimalSeparator: ",",
  groupSeparator: " ",
  templates: ["{} €"],
});                                      // "-12 345 678,90 €"

const opts = { templates: ["${}", "$({})", "-"] };
BigAmount.create("123.45").toFixed(2, opts); // "$123.45"
BigAmount.create("-678.9").toFixed(2, opts); // "$(678.90)"
BigAmount.create("0").toFixed(2, opts);      // "-"

toExponential() returns a string representing a fraction in exponential notation.

q("12345.678").toExponential(6); // "1.234568e+4"

License

Licensed under the Apache License, Version 2.0.

See Also