MCPcopy Create free account
hub / github.com/Effect-TS/effect / format

Function format

packages/effect/src/BigDecimal.ts:1381–1410  ·  view source on GitHub ↗
(n: BigDecimal)

Source from the content-addressed store, hash-verified

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.

Callers 2

toStringFunction · 0.70
toNumberUnsafeFunction · 0.70

Calls 2

toExponentialFunction · 0.85
normalizeFunction · 0.70

Tested by

no test coverage detected