Hyperlinkv0.8.0-beta.28

BigDecimal

BigDecimal.sumAllconsteffect/BigDecimal.ts:346
(collection: Iterable<BigDecimal>): BigDecimal

Takes an Iterable of BigDecimals and returns their sum as a single BigDecimal.

When to use

Use when you need to aggregate decimal quantities with decimal precision instead of converting through JavaScript numbers.

Example (Adding multiple decimals)

import { BigDecimal } from "effect"
import * as assert from "node:assert"

assert.deepStrictEqual(
  BigDecimal.sumAll([BigDecimal.fromStringUnsafe("2"), BigDecimal.fromStringUnsafe("3"), BigDecimal.fromStringUnsafe("4")]),
  BigDecimal.fromStringUnsafe("9")
)
mathsum
export const sumAll = (collection: Iterable<BigDecimal>): BigDecimal => {
  let out: BigDecimal = zero
  for (const n of collection) {
    out = sum(out, n)
  }
  return out
}