Hyperlinkv0.8.0-beta.28

BigDecimal

BigDecimal.toExponentialconsteffect/BigDecimal.ts:1537
(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")
convertingformat
export const toExponential = (n: BigDecimal): string => {
  if (isZero(n)) {
    return "0e+0"
  }

  const normalized = normalize(n)
  const digits = `${abs(normalized).value}`
  const head = digits.slice(0, 1)
  const tail = digits.slice(1)

  let output = `${isNegative(normalized) ? "-" : ""}${head}`
  if (tail !== "") {
    output += `.${tail}`
  }

  const exp = tail.length - normalized.scale
  return `${output}e${exp >= 0 ? "+" : ""}${exp}`
}
Referenced by 1 symbols