toString (Decimal)

stripe-node · src/Decimal.ts · typescript · MIT
payments, tag:decimal-handling, tag:number-formatting, tag:arbitrary-precision, tag:scientific-notation

Converts a Decimal value to its string representation, handling plain and scientific notation with safeguards against excessive memory use for extreme exponents.

// 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.
  toString(): string {
    if (this._coefficient === 0n) {
      return '0';
    }

    const coeffStr = this._coefficient.toString();
    const isNeg = coeffStr.startsWith('-');
    const absCoeffStr = isNeg ? coeffStr.slice(1) : coeffStr;

    if (this._exponent < 0) {
      const decimalPlaces = -this._exponent;

      // Guard against unbounded string allocation for extreme negative
      // exponents (e.g. 1e-1000000 would otherwise produce a million-char
      // string of leading zeros). Switch to scientific notation when the
      // number of leading zeros alone exceeds the digit limit. Normal
      // fractional values 

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

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

Get a free API key