MCPcopy Index your code
hub / github.com/Effect-TS/effect / format

Function format

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

Source from the content-addressed store, hash-verified

975 * @category conversions
976 */
977export const format = (n: BigDecimal): string => {
978 const normalized = normalize(n)
979 if (Math.abs(normalized.scale) >= 16) {
980 return toExponential(normalized)
981 }
982
983 const negative = normalized.value < bigint0
984 const absolute = negative ? `${normalized.value}`.substring(1) : `${normalized.value}`
985
986 let before: string
987 let after: string
988
989 if (normalized.scale >= absolute.length) {
990 before = "0"
991 after = "0".repeat(normalized.scale - absolute.length) + absolute
992 } else {
993 const location = absolute.length - normalized.scale
994 if (location > absolute.length) {
995 const zeros = location - absolute.length
996 before = `${absolute}${"0".repeat(zeros)}`
997 after = ""
998 } else {
999 after = absolute.slice(location)
1000 before = absolute.slice(0, location)
1001 }
1002 }
1003
1004 const complete = after === "" ? before : `${before}.${after}`
1005 return negative ? `-${complete}` : complete
1006}
1007
1008/**
1009 * Formats a given `BigDecimal` as a `string` in scientific notation.

Callers 2

toStringFunction · 0.70
unsafeToNumberFunction · 0.70

Calls 2

toExponentialFunction · 0.85
normalizeFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…