mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-26 16:55:15 +00:00
Merge pull request #467 from zenixls2/fix/fixedpoint_empty_check
fix: exception on parsing empty string in dnum
This commit is contained in:
commit
62b6f7bf6f
|
@ -491,6 +491,9 @@ func (v *Value) Scan(src interface{}) error {
|
||||||
// NewFromString parses a numeric string and returns a Value representation.
|
// NewFromString parses a numeric string and returns a Value representation.
|
||||||
func NewFromString(s string) (Value, error) {
|
func NewFromString(s string) (Value, error) {
|
||||||
length := len(s)
|
length := len(s)
|
||||||
|
if length == 0 {
|
||||||
|
return Zero, nil
|
||||||
|
}
|
||||||
isPercentage := s[length-1] == '%'
|
isPercentage := s[length-1] == '%'
|
||||||
if isPercentage {
|
if isPercentage {
|
||||||
s = s[:length-1]
|
s = s[:length-1]
|
||||||
|
@ -538,6 +541,9 @@ func MustNewFromString(input string) Value {
|
||||||
|
|
||||||
func NewFromBytes(s []byte) (Value, error) {
|
func NewFromBytes(s []byte) (Value, error) {
|
||||||
length := len(s)
|
length := len(s)
|
||||||
|
if length == 0 {
|
||||||
|
return Zero, nil
|
||||||
|
}
|
||||||
isPercentage := s[length-1] == '%'
|
isPercentage := s[length-1] == '%'
|
||||||
if isPercentage {
|
if isPercentage {
|
||||||
s = s[:length-1]
|
s = s[:length-1]
|
||||||
|
|
|
@ -126,6 +126,8 @@ func TestFromString(t *testing.T) {
|
||||||
assert.Equal(t, "0.00000011", f.String())
|
assert.Equal(t, "0.00000011", f.String())
|
||||||
f = MustNewFromString(".0%")
|
f = MustNewFromString(".0%")
|
||||||
assert.Equal(t, Zero, f)
|
assert.Equal(t, Zero, f)
|
||||||
|
f = MustNewFromString("")
|
||||||
|
assert.Equal(t, Zero, f)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestJson(t *testing.T) {
|
func TestJson(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user