diff options
author | Ting-Wei Lan <tingwei.lan@cobinhood.com> | 2019-02-19 17:00:26 +0800 |
---|---|---|
committer | lantw44 <lantw44@gmail.com> | 2019-03-27 10:24:09 +0800 |
commit | 077dad5493da2935813e248241694f4a99837c3e (patch) | |
tree | d7c8b83c23ec6b50a7093a84a1da6f89ab3d008f | |
parent | 06bfa5d7fe752784170b7362c0bcf490382babd9 (diff) | |
download | dexon-decimal-077dad5493da2935813e248241694f4a99837c3e.tar.gz dexon-decimal-077dad5493da2935813e248241694f4a99837c3e.tar.zst dexon-decimal-077dad5493da2935813e248241694f4a99837c3e.zip |
Fix decoding of .0
Since trailing 0 digits after the decimal point are trimmed, intString
becomes an empty string when there is no digit before the decimal point
and all digits after the decimal point is 0, causing big.Int SetString
to fail because there is no string to parse.
Fix #134.
-rw-r--r-- | decimal.go | 3 | ||||
-rw-r--r-- | decimal_test.go | 2 |
2 files changed, 5 insertions, 0 deletions
@@ -133,6 +133,9 @@ func NewFromString(value string) (Decimal, error) { // strip the insignificant digits for more accurate comparisons. decimalPart := strings.TrimRight(parts[1], "0") intString = parts[0] + decimalPart + if intString == "" && parts[1] != "" { + intString = "0" + } expInt := -len(decimalPart) exp += int64(expInt) } else { diff --git a/decimal_test.go b/decimal_test.go index 64f0552..395357f 100644 --- a/decimal_test.go +++ b/decimal_test.go @@ -70,6 +70,8 @@ var testTableScientificNotation = map[string]string{ "1.2345E-1": "0.12345", "0e5": "0", "0e-5": "0", + "0.e0": "0", + ".0e0": "0", "123.456e0": "123.456", "123.456e2": "12345.6", "123.456e10": "1234560000000", |