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

expression: function format incompatible #9182

Merged
merged 14 commits into from Jan 31, 2019
48 changes: 48 additions & 0 deletions expression/builtin_string.go
Expand Up @@ -3019,6 +3019,54 @@ func (b *builtinFormatSig) evalString(row chunk.Row) (string, bool, error) {
if isNull || err != nil {
return "", isNull, err
}
if strings.Contains(x, ".") {
eurekaka marked this conversation as resolved.
Show resolved Hide resolved
xArr := strings.Split(x, ".")
x1 := xArr[0]
x2 := xArr[1]
digit, err := strconv.Atoi(d)
if err != nil {
return "", isNull, err
}
if len(x2) > digit {
t := []byte(x2)
carry := false
if t[digit] >= '5' {
carry = true
}
if carry == true {
eurekaka marked this conversation as resolved.
Show resolved Hide resolved
for i := digit - 1; i >= 0; i-- {
if t[i] == '9' && carry == true {
t[i] = '0'
carry = true
} else if carry == true {
t[i] = t[i] + 1
carry = false
eurekaka marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
x2 = string(t)
t = []byte(x1)
if carry == true {
for i := len(x1) - 1; i >= 0; i-- {
if t[i] == '9' && carry == true {
t[i] = '0'
carry = true
} else if carry == true {
t[i] = t[i] + 1
carry = false
eurekaka marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
if carry == true {
x1 = "1" + string(t)
} else {
x1 = string(t)
}
}

x = x1 + "." + x2
}

formatString, err := mysql.GetLocaleFormatFunction("en_US")(x, d)
return formatString, false, err
}
Expand Down
6 changes: 6 additions & 0 deletions expression/builtin_string_test.go
Expand Up @@ -1608,6 +1608,12 @@ func (s *testEvaluatorSuite) TestFormat(c *C) {
ret interface{}
warnings int
}{
// issue #8796
{1.12345, 4, "1.1235", 0},
{9.99999, 4, "10.0000", 0},
{1.99999, 4, "2.0000", 0},
{1.09999, 4, "1.1000", 0},

{12332.123444, 4, "12,332.1234", 0},
{12332.123444, 0, "12,332", 0},
{12332.123444, -4, "12,332", 0},
Expand Down