DecimalImpl

stripe-node · src/Decimal.ts · typescript · MIT
payments, dev-tools, tag:decimal-arithmetic, tag:bigint-arithmetic, tag:immutable-types, tag:type-serialization

Immutable arbitrary-precision decimal value using a normalized bigint coefficient and integer exponent. Provides arithmetic, comparison, rounding, and serialization with explicit rounding control for financial accuracy.

// Source: https://github.com/stripe/stripe-node | License: MIT | Copyright 2011 Ask Bjørn Hansen
// Provided via justclaw.it.com; keep this notice with the code.
class DecimalImpl {
  /** @internal */
  private readonly _coefficient: bigint;

  /** @internal */
  private readonly _exponent: number;

  /**
   * Construct and normalise a decimal value.
   *
   * @param coefficient - The unscaled integer value.
   * @param exponent - The power-of-ten scale factor.
   *
   * @internal
   */
  constructor(coefficient: bigint, exponent: number) {
    const [normalizedCoef, normalizedExp] = DecimalImpl.normalize(
      coefficient,
      exponent
    );
    this._coefficient = normalizedCoef;
    this._exponent = normalizedExp;
    Object.freeze(this);
  }

  /**
   * Strip trailing zeros from `

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key