mirror of
https://github.com/Such-Software/hash-wallet.git
synced 2026-12-02 12:14:09 +01:00
refactor: improve parseFixed logic and add comprehensive unit tests for edge cases (#2661)
This commit is contained in:
@@ -1,14 +1,13 @@
|
||||
BigInt parseFixed(String value, int? decimals) {
|
||||
decimals ??= 0;
|
||||
BigInt parseFixed(String value, int decimals) {
|
||||
final multiplier = getMultiplier(decimals);
|
||||
|
||||
// Is it negative?
|
||||
final negative = (value.substring(0, 1) == "-");
|
||||
final negative = value.startsWith("-");
|
||||
if (negative) value = value.substring(1);
|
||||
|
||||
if (value == ".") throw Exception("missing value, value, $value");
|
||||
|
||||
// Split it into a whole and fractional part
|
||||
if (value.startsWith(".")) value = "0$value";
|
||||
|
||||
final comps = value.split(".");
|
||||
if (comps.length > 2) {
|
||||
throw Exception("too many decimal points, value, $value");
|
||||
@@ -17,7 +16,6 @@ BigInt parseFixed(String value, int? decimals) {
|
||||
var whole = comps.isNotEmpty ? comps[0] : "0";
|
||||
var fraction = (comps.length == 2 ? comps[1] : "0").padRight(decimals, "0");
|
||||
|
||||
// Check the fraction doesn't exceed our decimals size
|
||||
if (fraction.length > multiplier.length - 1) {
|
||||
throw Exception(
|
||||
"fractional component exceeds decimals, underflow, parseFixed");
|
||||
|
||||
Reference in New Issue
Block a user