* 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)
| 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%%. |
no test coverage detected