Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mysqldef: support parsing scientific notation #306

Merged
merged 2 commits into from
Oct 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions mysqldef/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,17 +199,28 @@ func NewDecimalFromUint(value uint64, exp int32) Decimal {
//
func ParseDecimal(value string) (Decimal, error) {
var intString string
var exp int32
var exp = int32(0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Origin code is also int32(0)?


n := strings.IndexAny(value, "eE")
if n > 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about "e1"?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

invalid format

// It is scientific notation, like 3.14e10
expInt, err := strconv.Atoi(value[n+1:])
if err != nil {
return Decimal{}, fmt.Errorf("can't convert %s to decimal, incorrect exponent", value)
}
value = value[0:n]
exp = int32(expInt)
}

parts := strings.Split(value, ".")
if len(parts) == 1 {
// There is no decimal point, we can just parse the original string as
// an int.
intString = value
exp = 0
} else if len(parts) == 2 {
intString = parts[0] + parts[1]
expInt := -len(parts[1])
exp = int32(expInt)
exp += int32(expInt)
} else {
return Decimal{}, fmt.Errorf("can't convert %s to decimal: too many .s", value)
}
Expand Down
40 changes: 40 additions & 0 deletions mysqldef/decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -869,3 +869,43 @@ func didPanic(f func()) bool {

return ret
}

func TestDecimalScientificNotation(t *testing.T) {
tbl := []struct {
Input string
Expected float64
}{
{"314e-2", 3.14},
{"1e2", 100},
{"2E-1", 0.2},
{"2E0", 2},
{"2.2E-1", 0.22},
}

for _, c := range tbl {
n, err := ParseDecimal(c.Input)
if err != nil {
t.Error(err)
}

f, _ := n.Float64()
if f != c.Expected {
t.Errorf("%f != %f", f, c.Expected)
}
}

tblErr := []string{
"12ee",
"ae10",
"12e1a",
"12e1.2",
"e1",
}

for _, c := range tblErr {
_, err := ParseDecimal(c)
if err == nil {
t.Errorf("%s must be invalid decimal", c)
}
}
}