aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTing-Wei Lan <tingwei.lan@cobinhood.com>2019-02-19 17:00:26 +0800
committerlantw44 <lantw44@gmail.com>2019-03-27 10:24:09 +0800
commit077dad5493da2935813e248241694f4a99837c3e (patch)
treed7c8b83c23ec6b50a7093a84a1da6f89ab3d008f
parent06bfa5d7fe752784170b7362c0bcf490382babd9 (diff)
downloaddexon-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.go3
-rw-r--r--decimal_test.go2
2 files changed, 5 insertions, 0 deletions
diff --git a/decimal.go b/decimal.go
index 763c485..0f7f18f 100644
--- a/decimal.go
+++ b/decimal.go
@@ -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",