roundDivision (Decimal)

stripe-node · src/Decimal.ts · typescript · MIT
dev-tools, tag:decimal-handling, tag:arbitrary-precision, tag:rounding-modes, tag:integer-division

Performs arbitrary-precision integer division with configurable rounding modes (up, down, ceil, floor, half-up, half-down, half-even), handling remainders and tie-breaking for exact decimal arithmetic.

// 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.
  private static roundDivision(
    quotient: bigint,
    remainder: bigint,
    divisor: bigint,
    direction: RoundDirection
  ): bigint {
    if (remainder === 0n) {
      return quotient;
    }

    // 'round-down': truncate toward zero — BigInt division already does this.
    if (direction === 'round-down') {
      return quotient;
    }

    // The sign of remainder/divisor tells us which side of the truncation
    // point the exact value lies on.
    // Same sign → fractional part is positive (exact value > quotient) → +1 adjusts upward.
    // Opposite sign → fractional part is negative (exact value < quotient) → -1 adj

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

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

Get a free API key