Hyperlinkv0.8.0-beta.28

BigDecimal

BigDecimal.truncateconsteffect/BigDecimal.ts:1825
(scale: number): (self: BigDecimal) => BigDecimal
(self: BigDecimal, scale?: number): BigDecimal

Computes a truncated BigDecimal at the given scale. This removes fractional digits beyond the scale, rounding toward zero.

When to use

Use when you need to discard fractional digits beyond a scale rather than round half up, half down, or toward an infinity.

Example (Truncating decimals)

import { BigDecimal } from "effect"

console.log(BigDecimal.truncate(BigDecimal.fromStringUnsafe("145"), -1)) // BigDecimal(140)
console.log(BigDecimal.truncate(BigDecimal.fromStringUnsafe("-14.5"))) // BigDecimal(-14)
export const truncate: {
  (scale: number): (self: BigDecimal) => BigDecimal
  (self: BigDecimal, scale?: number): BigDecimal
} = dual(isBigDecimalArgs, (self: BigDecimal, scale: number = 0): BigDecimal => {
  if (self.scale <= scale) {
    return self
  }

  // BigInt division truncates towards zero
  return make(self.value / (bigint10 ** BigInt(self.scale - scale)), scale)
})
Referenced by 3 symbols