mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
fix issue in minus number converting for gconv.Int
This commit is contained in:
@ -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"},
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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))
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user