Hyperlinkv0.8.0-beta.28

BigDecimal

BigDecimal.Orderconsteffect/BigDecimal.ts:683
(self: BigDecimal, that: BigDecimal): Ordering

Provides an Order instance for BigDecimal that allows comparing and sorting BigDecimal values.

When to use

Use when you need to sort or compare decimal values through APIs that accept an ordering instance.

Example (Comparing decimals)

import { BigDecimal } from "effect"

const a = BigDecimal.fromNumberUnsafe(1.5)
const b = BigDecimal.fromNumberUnsafe(2.3)
const c = BigDecimal.fromNumberUnsafe(1.5)

console.log(BigDecimal.Order(a, b)) // -1 (a < b)
console.log(BigDecimal.Order(b, a)) // 1 (b > a)
console.log(BigDecimal.Order(a, c)) // 0 (a === c)
instances
export const Order: order.Order<BigDecimal> = order.make((self, that) => {
  const scmp = order.Number(sign(self), sign(that))
  if (scmp !== 0) {
    return scmp
  }

  if (self.scale > that.scale) {
    return order.BigInt(self.value, scale(that, self.scale).value)
  }

  if (self.scale < that.scale) {
    return order.BigInt(scale(self, that.scale).value, that.value)
  }

  return order.BigInt(self.value, that.value)
})
Referenced by 14 symbols