(n: BigDecimal): stringFormats a BigDecimal as a string.
When to use
Use to render a BigDecimal as plain decimal text when possible.
Details
The value is normalized before formatting. Scientific notation is used when
the absolute value of the normalized scale is at least 16; otherwise plain
decimal notation is used.
Example (Formatting decimals)
import { BigDecimal } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(BigDecimal.format(BigDecimal.fromStringUnsafe("-5")), "-5")
assert.deepStrictEqual(BigDecimal.format(BigDecimal.fromStringUnsafe("123.456")), "123.456")
assert.deepStrictEqual(BigDecimal.format(BigDecimal.fromStringUnsafe("-0.00000123")), "-0.00000123")export const const format: (n: BigDecimal) => stringFormats a BigDecimal as a string.
When to use
Use to render a BigDecimal as plain decimal text when possible.
Details
The value is normalized before formatting. Scientific notation is used when
the absolute value of the normalized scale is at least 16; otherwise plain
decimal notation is used.
Example (Formatting decimals)
import { BigDecimal } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(BigDecimal.format(BigDecimal.fromStringUnsafe("-5")), "-5")
assert.deepStrictEqual(BigDecimal.format(BigDecimal.fromStringUnsafe("123.456")), "123.456")
assert.deepStrictEqual(BigDecimal.format(BigDecimal.fromStringUnsafe("-0.00000123")), "-0.00000123")
format = (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 => {
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)
if (var Math: MathAn intrinsic object that provides basic mathematics functionality and constants.
Math.Math.abs(x: number): numberReturns the absolute value of a number (the value without regard to whether it is positive or negative).
For example, the absolute value of -5 is the same as the absolute value of 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.scale: numberscale) >= 16) {
return 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(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 const negative: booleannegative = 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 bigint0: bigintbigint0
const const absolute: stringabsolute = const negative: booleannegative ? `${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}`.String.substring(start: number, end?: number): stringReturns the substring at the specified location within a String object.
substring(1) : `${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}`
let let before: stringbefore: string
let let after: stringafter: string
if (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 >= const absolute: stringabsolute.String.length: numberReturns the length of a String object.
length) {
let before: stringbefore = "0"
let after: stringafter = "0".String.repeat(count: number): stringReturns a String value that is made from count copies appended together. If count is 0,
the empty string is returned.
repeat(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 - const absolute: stringabsolute.String.length: numberReturns the length of a String object.
length) + const absolute: stringabsolute
} else {
const const location: numberlocation = const absolute: stringabsolute.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
if (const location: numberlocation > const absolute: stringabsolute.String.length: numberReturns the length of a String object.
length) {
const const zeros: numberzeros = const location: numberlocation - const absolute: stringabsolute.String.length: numberReturns the length of a String object.
length
let before: stringbefore = `${const absolute: stringabsolute}${"0".String.repeat(count: number): stringReturns a String value that is made from count copies appended together. If count is 0,
the empty string is returned.
repeat(const zeros: numberzeros)}`
let after: stringafter = ""
} else {
let after: stringafter = const absolute: stringabsolute.String.slice(start?: number, end?: number): stringReturns a section of a string.
slice(const location: numberlocation)
let before: stringbefore = const absolute: stringabsolute.String.slice(start?: number, end?: number): stringReturns a section of a string.
slice(0, const location: numberlocation)
}
}
const const complete: stringcomplete = let after: stringafter === "" ? let before: stringbefore : `${let before: stringbefore}.${let after: stringafter}`
return const negative: booleannegative ? `-${const complete: stringcomplete}` : const complete: stringcomplete
}