Hyperlinkv0.8.0-beta.28

BigDecimal

BigDecimal.subtractconsteffect/BigDecimal.ts:446
(that: BigDecimal): (self: BigDecimal) => BigDecimal
(self: BigDecimal, that: BigDecimal): BigDecimal

Provides a subtraction operation on BigDecimals.

When to use

Use to subtract one BigDecimal value from another.

Example (Subtracting decimals)

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

assert.deepStrictEqual(
  BigDecimal.subtract(BigDecimal.fromStringUnsafe("2"), BigDecimal.fromStringUnsafe("3")),
  BigDecimal.fromStringUnsafe("-1")
)
math
export const subtract: {
  (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 make(-that.value, that.scale)
  }

  if (self.scale > that.scale) {
    return make(self.value - scale(that, self.scale).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)
})