Hyperlinkv0.8.0-beta.28

BigDecimal

BigDecimal.scaleconsteffect/BigDecimal.ts:258
(scale: number): (self: BigDecimal) => BigDecimal
(self: BigDecimal, scale: number): BigDecimal

Changes a BigDecimal to the specified scale.

When to use

Use to change how many decimal places are represented by a BigDecimal.

Details

Increasing the scale appends decimal zeros. Decreasing the scale discards digits beyond the target scale by bigint division, which truncates toward zero.

Example (Scaling decimal precision)

import { BigDecimal } from "effect"

const decimal = BigDecimal.fromNumberUnsafe(123.45)

// Increase scale (add more precision)
const scaled = BigDecimal.scale(decimal, 4)
console.log(BigDecimal.format(scaled)) // "123.4500"

// Decrease scale (reduce precision, rounds down)
const reduced = BigDecimal.scale(decimal, 1)
console.log(BigDecimal.format(reduced)) // "123.4"
scalinground
export const scale: {
  (scale: number): (self: BigDecimal) => BigDecimal
  (self: BigDecimal, scale: number): BigDecimal
} = dual(2, (self: BigDecimal, scale: number): BigDecimal => {
  if (scale > self.scale) {
    return make(self.value * bigint10 ** BigInt(scale - self.scale), scale)
  }

  if (scale < self.scale) {
    return make(self.value / bigint10 ** BigInt(self.scale - scale), scale)
  }

  return self
})
Referenced by 7 symbols