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

Method fromString

src.ts/utils/fixednumber.ts:595–619  ·  view source on GitHub ↗

* Creates a new [[FixedNumber]] for %%value%% with %%format%%. * * This will throw a [[NumericFaultError]] if %%value%% cannot fit * in %%format%%, either due to overflow or underflow (precision loss).

(_value: string, _format?: FixedFormat)

Source from the content-addressed store, hash-verified

593 * in %%format%%, either due to overflow or underflow (precision loss).
594 */
595 static fromString(_value: string, _format?: FixedFormat): FixedNumber {
596 const match = _value.match(/^(-?)([0-9]*)\.?([0-9]*)$/);
597 assertArgument(match && (match[2].length + match[3].length) > 0, "invalid FixedNumber string value", "value", _value);
598
599 const format = getFormat(_format);
600
601 let whole = (match[2] || "0"), decimal = (match[3] || "");
602
603 // Pad out the decimals
604 while (decimal.length < format.decimals) { decimal += Zeros; }
605
606 // Check precision is safe
607 assert(decimal.substring(format.decimals).match(/^0*$/), "too many decimals for format", "NUMERIC_FAULT", {
608 operation: "fromString", fault: "underflow", value: _value
609 });
610
611 // Remove extra padding
612 decimal = decimal.substring(0, format.decimals);
613
614 const value = BigInt(match[1] + whole + decimal)
615
616 checkValue(value, format, "fromString");
617
618 return new FixedNumber(_guard, value, format);
619 }
620
621 /**
622 * Creates a new [[FixedNumber]] with the big-endian representation

Callers 2

parseUnitsFunction · 0.45
toFormatMethod · 0.45

Calls 4

assertArgumentFunction · 0.90
assertFunction · 0.90
getFormatFunction · 0.70
checkValueFunction · 0.70

Tested by

no test coverage detected