Hyperlinkv0.8.0-beta.28

BigDecimal

BigDecimal.floorconsteffect/BigDecimal.ts:1932
(scale: number): (self: BigDecimal) => BigDecimal
(self: BigDecimal, scale?: number): BigDecimal

Computes the floor of a BigDecimal at the given scale.

When to use

Use to round a decimal toward negative infinity at a requested scale.

Example (Rounding decimals down)

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

assert.deepStrictEqual(
  BigDecimal.floor(BigDecimal.fromStringUnsafe("145"), -1),
  BigDecimal.fromStringUnsafe("140")
)
assert.deepStrictEqual(
  BigDecimal.floor(BigDecimal.fromStringUnsafe("-14.5")),
  BigDecimal.fromStringUnsafe("-15")
)
export const floor: {
  (scale: number): (self: BigDecimal) => BigDecimal
  (self: BigDecimal, scale?: number): BigDecimal
} = dual(isBigDecimalArgs, (self: BigDecimal, scale: number = 0): BigDecimal => {
  const truncated = truncate(self, scale)

  if (isNegative(self) && isGreaterThan(truncated, self)) {
    return sum(truncated, make(bigint_1, scale))
  }

  return truncated
})
Referenced by 2 symbols