(s: string): BigDecimalParses a decimal string into a BigDecimal, throwing if the string is
invalid.
When to use
Use when you expect decimal text to be valid and want parse errors to throw.
Details
Accepts the same syntax as fromString. Use fromString when invalid input
should be represented as Option.none instead of throwing.
Example (Parsing decimal strings unsafely)
import { BigDecimal } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(BigDecimal.fromStringUnsafe("123"), BigDecimal.make(123n, 0))
assert.deepStrictEqual(BigDecimal.fromStringUnsafe("123.456"), BigDecimal.make(123456n, 3))
assert.throws(() => BigDecimal.fromStringUnsafe("123.abc"))export const const fromStringUnsafe: (
s: string
) => BigDecimal
Parses a decimal string into a BigDecimal, throwing if the string is
invalid.
When to use
Use when you expect decimal text to be valid and want parse errors to throw.
Details
Accepts the same syntax as fromString. Use fromString when invalid input
should be represented as Option.none instead of throwing.
Example (Parsing decimal strings unsafely)
import { BigDecimal } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(BigDecimal.fromStringUnsafe("123"), BigDecimal.make(123n, 0))
assert.deepStrictEqual(BigDecimal.fromStringUnsafe("123.456"), BigDecimal.make(123456n, 3))
assert.throws(() => BigDecimal.fromStringUnsafe("123.abc"))
fromStringUnsafe = (s: strings: string): BigDecimal => {
return import OptionOption.const getOrThrowWith: {
(onNone: () => unknown): <A>(
self: Option<A>
) => A
<A>(self: Option<A>, onNone: () => unknown): A
}
getOrThrowWith(const fromString: (
s: string
) => Option.Option<BigDecimal>
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())
fromString(s: strings), () => new var Error: ErrorConstructor
new (message?: string, options?: ErrorOptions) => Error (+1 overload)
Error(`Invalid numerical string: ${s: strings}`))
}