MCPcopy Create free account
hub / github.com/PageLeay/celestial-runtime / toString

Function toString

lib.esm/utils/fixednumber.js:98–125  ·  view source on GitHub ↗
(val, decimals)

Source from the content-addressed store, hash-verified

96 return { signed, width, decimals, name };
97}
98function toString(val, decimals) {
99 let negative = "";
100 if (val < BN_0) {
101 negative = "-";
102 val *= BN_N1;
103 }
104 let str = val.toString();
105 // No decimal point for whole values
106 if (decimals === 0) {
107 return (negative + str);
108 }
109 // Pad out to the whole component (including a whole digit)
110 while (str.length <= decimals) {
111 str = Zeros + str;
112 }
113 // Insert the decimal point
114 const index = str.length - decimals;
115 str = str.substring(0, index) + "." + str.substring(index);
116 // Trim the whole component (leaving at least one 0)
117 while (str[0] === "0" && str[1] !== ".") {
118 str = str.substring(1);
119 }
120 // Trim the decimal component (leaving at least one 0)
121 while (str[str.length - 1] === "0" && str[str.length - 2] !== ".") {
122 str = str.substring(0, str.length - 1);
123 }
124 return (negative + str);
125}
126/**
127 * A FixedNumber represents a value over its [[FixedFormat]]
128 * arithmetic field.

Callers 1

constructorMethod · 0.70

Calls 1

toStringMethod · 0.45

Tested by

no test coverage detected