[common] Allow capital E in scientific notation for amount input (#227)

pull/228/head
Janet Liang 5 years ago committed by GitHub
parent 6a588b9529
commit 854a73eba6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      pkg/common/numeric.go

@ -12,7 +12,7 @@ import (
)
var (
pattern, _ = regexp.Compile("[0-9]+\\.{0,1}[0-9]*e-{0,1}[0-9]+")
pattern, _ = regexp.Compile("[0-9]+\\.{0,1}[0-9]*[eE]-{0,1}[0-9]+")
)
func Pow(base numeric.Dec, exp int) numeric.Dec {
@ -38,7 +38,12 @@ func NewDecFromString(i string) (numeric.Dec, error) {
return numeric.ZeroDec(), errors.New(fmt.Sprintf("can not be negative: %s", i))
}
if pattern.FindString(i) != "" {
tokens := strings.Split(i, "e")
if tokens := strings.Split(i, "e"); len(tokens) > 1 {
a, _ := numeric.NewDecFromStr(tokens[0])
b, _ := strconv.Atoi(tokens[1])
return a.Mul(Pow(numeric.NewDec(10), b)), nil
}
tokens := strings.Split(i, "E")
a, _ := numeric.NewDecFromStr(tokens[0])
b, _ := strconv.Atoi(tokens[1])
return a.Mul(Pow(numeric.NewDec(10), b)), nil

Loading…
Cancel
Save