(n: BigDecimal): stringFormats a given BigDecimal as a string in scientific notation.
When to use
Use to render a BigDecimal in scientific notation.
Example (Formatting decimals exponentially)
import { BigDecimal } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(BigDecimal.toExponential(BigDecimal.make(123456n, -5)), "1.23456e+10")export const const toExponential: (
n: BigDecimal
) => string
Formats a given BigDecimal as a string in scientific notation.
When to use
Use to render a BigDecimal in scientific notation.
Example (Formatting decimals exponentially)
import { BigDecimal } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(BigDecimal.toExponential(BigDecimal.make(123456n, -5)), "1.23456e+10")
toExponential = (n: BigDecimal(parameter) n: {
value: bigint;
scale: number;
normalized: BigDecimal;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
n: BigDecimal): string => {
if (const isZero: (n: BigDecimal) => booleanChecks whether a given BigDecimal is 0.
When to use
Use to test whether a BigDecimal is exactly zero.
Example (Checking zero decimals)
import { BigDecimal } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(BigDecimal.isZero(BigDecimal.fromStringUnsafe("0")), true)
assert.deepStrictEqual(BigDecimal.isZero(BigDecimal.fromStringUnsafe("1")), false)
isZero(n: BigDecimal(parameter) n: {
value: bigint;
scale: number;
normalized: BigDecimal;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
n)) {
return "0e+0"
}
const const normalized: BigDecimalconst normalized: {
value: bigint;
scale: number;
normalized: BigDecimal;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
normalized = const normalize: (
self: BigDecimal
) => BigDecimal
Normalizes a given BigDecimal by removing trailing zeros.
When to use
Use to canonicalize decimals that have equivalent values but different
internal scales.
Example (Normalizing trailing zeros)
import { BigDecimal } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(
BigDecimal.normalize(BigDecimal.fromStringUnsafe("123.00000")),
BigDecimal.normalize(BigDecimal.make(123n, 0))
)
assert.deepStrictEqual(
BigDecimal.normalize(BigDecimal.fromStringUnsafe("12300000")),
BigDecimal.normalize(BigDecimal.make(123n, -5))
)
normalize(n: BigDecimal(parameter) n: {
value: bigint;
scale: number;
normalized: BigDecimal;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
n)
const const digits: stringdigits = `${const abs: (n: BigDecimal) => BigDecimalDetermines the absolute value of a given BigDecimal.
When to use
Use to remove the sign from a BigDecimal while preserving its magnitude.
Example (Calculating absolute values)
import { BigDecimal } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(BigDecimal.abs(BigDecimal.fromStringUnsafe("-5")), BigDecimal.fromStringUnsafe("5"))
assert.deepStrictEqual(BigDecimal.abs(BigDecimal.fromStringUnsafe("0")), BigDecimal.fromStringUnsafe("0"))
assert.deepStrictEqual(BigDecimal.abs(BigDecimal.fromStringUnsafe("5")), BigDecimal.fromStringUnsafe("5"))
abs(const normalized: BigDecimalconst normalized: {
value: bigint;
scale: number;
normalized: BigDecimal;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
normalized).BigDecimal.value: bigintvalue}`
const const head: stringhead = const digits: stringdigits.String.slice(start?: number, end?: number): stringReturns a section of a string.
slice(0, 1)
const const tail: stringtail = const digits: stringdigits.String.slice(start?: number, end?: number): stringReturns a section of a string.
slice(1)
let let output: stringoutput = `${const isNegative: (
n: BigDecimal
) => boolean
Checks whether a given BigDecimal is negative.
When to use
Use to test whether a BigDecimal is less than zero.
Example (Checking negative decimals)
import { BigDecimal } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(BigDecimal.isNegative(BigDecimal.fromStringUnsafe("-1")), true)
assert.deepStrictEqual(BigDecimal.isNegative(BigDecimal.fromStringUnsafe("0")), false)
assert.deepStrictEqual(BigDecimal.isNegative(BigDecimal.fromStringUnsafe("1")), false)
isNegative(const normalized: BigDecimalconst normalized: {
value: bigint;
scale: number;
normalized: BigDecimal;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
normalized) ? "-" : ""}${const head: stringhead}`
if (const tail: stringtail !== "") {
let output: stringoutput += `.${const tail: stringtail}`
}
const const exp: numberexp = const tail: stringtail.String.length: numberReturns the length of a String object.
length - const normalized: BigDecimalconst normalized: {
value: bigint;
scale: number;
normalized: BigDecimal;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
normalized.BigDecimal.scale: numberscale
return `${let output: stringoutput}e${const exp: numberexp >= 0 ? "+" : ""}${const exp: numberexp}`
}