MCPcopy Create free account
hub / github.com/effect-app/libs / fromString

Function fromString

repos/effect/packages/effect/src/BigDecimal.ts:1378–1421  ·  view source on GitHub ↗
(s: string)

Source from the content-addressed store, hash-verified

1376 * @see {@link toExponential} for always rendering scientific notation
1377 *
1378 * @category converting
1379 * @since 2.0.0
1380 */
1381export const format = (n: BigDecimal): string => {
1382 const normalized = normalize(n)
1383 if (Math.abs(normalized.scale) >= 16) {
1384 return toExponential(normalized)
1385 }
1386
1387 const negative = normalized.value < bigint0
1388 const absolute = negative ? `${normalized.value}`.substring(1) : `${normalized.value}`
1389
1390 let before: string
1391 let after: string
1392
1393 if (normalized.scale >= absolute.length) {
1394 before = "0"
1395 after = "0".repeat(normalized.scale - absolute.length) + absolute
1396 } else {
1397 const location = absolute.length - normalized.scale
1398 if (location > absolute.length) {
1399 const zeros = location - absolute.length
1400 before = `${absolute}${"0".repeat(zeros)}`
1401 after = ""
1402 } else {
1403 after = absolute.slice(location)
1404 before = absolute.slice(0, location)
1405 }
1406 }
1407
1408 const complete = after === "" ? before : `${before}.${after}`
1409 return negative ? `-${complete}` : complete
1410}
1411
1412/**
1413 * Formats a given `BigDecimal` as a `string` in scientific notation.
1414 *
1415 * **When to use**
1416 *
1417 * Use to render a `BigDecimal` in scientific notation.
1418 *
1419 * **Example** (Formatting decimals exponentially)
1420 *
1421 * ```ts import.meta.vitest
1422 * import { BigDecimal } from "effect"
1423 *
1424 * BigDecimal.toExponential(BigDecimal.make(123456n, -5)) // => "1.23456e+10"

Callers 2

fromNumberFunction · 0.70
fromStringUnsafeFunction · 0.70

Calls 4

someMethod · 0.80
NumberInterface · 0.70
makeFunction · 0.70
BigIntInterface · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…