effect-io-ai

Package: effect
Module: BigDecimal

BigDecimal.fromString

Parses a decimal string into a BigDecimal safely.

When to use

Use to parse external decimal text without throwing on invalid input.

Details

Returns Option.some for valid decimal or exponent notation and Option.none when the string cannot be parsed or would produce an unsafe scale. The empty string parses as zero.

Example (Parsing decimal strings safely)

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

assert.deepStrictEqual(BigDecimal.fromString("123"), Option.some(BigDecimal.make(123n, 0)))
assert.deepStrictEqual(
  BigDecimal.fromString("123.456"),
  Option.some(BigDecimal.make(123456n, 3))
)
assert.deepStrictEqual(BigDecimal.fromString("123.abc"), Option.none())

See

Signature

declare const fromString: (s: string) => Option.Option<BigDecimal>

Source

Since v2.0.0