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

Method fromString

lib.esm/utils/fixednumber.js:486–504  ·  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, _format)

Source from the content-addressed store, hash-verified

484 * in %%format%%, either due to overflow or underflow (precision loss).
485 */
486 static fromString(_value, _format) {
487 const match = _value.match(/^(-?)([0-9]*)\.?([0-9]*)$/);
488 assertArgument(match && (match[2].length + match[3].length) > 0, "invalid FixedNumber string value", "value", _value);
489 const format = getFormat(_format);
490 let whole = (match[2] || "0"), decimal = (match[3] || "");
491 // Pad out the decimals
492 while (decimal.length < format.decimals) {
493 decimal += Zeros;
494 }
495 // Check precision is safe
496 assert(decimal.substring(format.decimals).match(/^0*$/), "too many decimals for format", "NUMERIC_FAULT", {
497 operation: "fromString", fault: "underflow", value: _value
498 });
499 // Remove extra padding
500 decimal = decimal.substring(0, format.decimals);
501 const value = BigInt(match[1] + whole + decimal);
502 checkValue(value, format, "fromString");
503 return new FixedNumber(_guard, value, format);
504 }
505 /**
506 * Creates a new [[FixedNumber]] with the big-endian representation
507 * %%value%% with %%format%%.

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