fix issue in minus number converting for gconv.Int

This commit is contained in:
John
2020-01-20 20:18:24 +08:00
parent eb6a7a4728
commit 665b5960c8
3 changed files with 25 additions and 0 deletions

View File

@ -13,6 +13,10 @@ type TokenRequest struct {
}
func main() {
s := "123456"
fmt.Println(s[0:2])
fmt.Println(s[1:3])
return
// s := `
//{
// "policy": {"name":"john"},

View File

@ -378,20 +378,36 @@ func Int64(i interface{}) int64 {
return gbinary.DecodeToInt64(value)
default:
s := String(value)
isMinus := false
if s[0] == '-' {
isMinus = true
s = s[1:]
} else if s[0] == '+' {
s = s[1:]
}
// Hexadecimal
if len(s) > 2 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X') {
if v, e := strconv.ParseInt(s[2:], 16, 64); e == nil {
if isMinus {
return -v
}
return v
}
}
// Octal
if len(s) > 1 && s[0] == '0' {
if v, e := strconv.ParseInt(s[1:], 8, 64); e == nil {
if isMinus {
return -v
}
return v
}
}
// Decimal
if v, e := strconv.ParseInt(s, 10, 64); e == nil {
if isMinus {
return -v
}
return v
}
// Float64

View File

@ -34,4 +34,9 @@ func Test_Basic(t *testing.T) {
gtest.AssertEQ(gconv.String(f32), "123.456")
gtest.AssertEQ(gconv.String(i64), "1552578474888")
})
gtest.Case(t, func() {
s := "-0xFF"
gtest.Assert(gconv.Int(s), int64(-0xFF))
})
}