Hyperlinkv0.8.0-beta.28

BigDecimal

BigDecimal.sumconsteffect/BigDecimal.ts:298
(that: BigDecimal): (self: BigDecimal) => BigDecimal
(self: BigDecimal, that: BigDecimal): BigDecimal

Provides an addition operation on BigDecimals.

When to use

Use when you need a decimal addition function for piping or higher-order APIs while preserving decimal precision.

Example (Adding decimals)

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

assert.deepStrictEqual(
  BigDecimal.sum(BigDecimal.fromStringUnsafe("2"), BigDecimal.fromStringUnsafe("3")),
  BigDecimal.fromStringUnsafe("5")
)
mathsumAll
export const sum: {
  (that: BigDecimal): (self: BigDecimal) => BigDecimal
  (self: BigDecimal, that: BigDecimal): BigDecimal
} = dual(2, (self: BigDecimal, that: BigDecimal): BigDecimal => {
  if (that.value === bigint0) {
    return self
  }

  if (self.value === bigint0) {
    return that
  }

  if (self.scale > that.scale) {
    return make(scale(that, self.scale).value + self.value, self.scale)
  }

  if (self.scale < that.scale) {
    return make(scale(self, that.scale).value + that.value, that.scale)
  }

  return make(self.value + that.value, self.scale)
})
Referenced by 4 symbols