Golang实例 - strconv FormatInt函数指南

3,375 阅读3分钟

在这篇博文中,我们将通过实例来学习Go语言中的strconv FormatInt函数

golangstrconvFormatInt函数

strconv是Go语言中的一个标准包,它提供了将字符串转换为int, float, boolean等类型的各种函数实现。

FormatInt是其中一个重要的函数,用于将整数类型转换为字符串类型。你可以在这里查看官方文档

以下是该函数的语法

func FormatInt(input int64, base int) string

参数列表

输入的是一个只属于int64类型的数字整数值

基数是int参数,其有效值为2到36。

返回类型

返回包含数字值的字符串值。

函数语法

FormatInt()函数接受并解析整数类型,并根据基值进行转换,返回一个字符串类型的值。

如果基数小于等于2,即1,0或负数,则会抛出运行时错误 - panic: strconv: illegal AppendInt/FormatInt base

完整的堆栈跟踪

panic: strconv: illegal AppendInt/FormatInt base  
  
goroutine 1 [running]:  
strconv.formatBits(0x0, 0x0, 0x0, 0x46e, 0x1, 0x65514b0fc43a0000, 0xc000069eb0, 0x8, 0x10, 0xc0000341c0, ...)  
        A:/Golang/src/strconv/itoa.go:91 +0x51c  
strconv.FormatInt(0x46e, 0x1, 0x9, 0xc000048238)  
        A:/Golang/src/strconv/itoa.go:29 +0xd2  
main.main()  
        A:/Golang/work/Test.go:12 +0x4c  
exit status 2  

如果基数大于2,它将整数转换为只包含0和1的二进制字符串。

s0 := strconv.FormatInt(55, 2)  
fmt.Printf("type=%T, output=%v\n", s0, s0) //type=string, output=110111  

如果基数大于10,输出包含小写字母'a'到'z',这是十六进制数字的字符串表示。

s0 := strconv.FormatInt(1234, 16)  
 fmt.Printf("type=%T, output=%v\n", s0, s0) //type=string, output=46e  

下面是一个将整数转换为不同字符串数值的例子

package main  
  
import (  
 "fmt"  
 "strconv"  
)  
  
func main() {  
 input := int64(1134)  
 fmt.Printf("type=%T, output=%v\n", input, input)  
 s0 := strconv.FormatInt(input, 2)  
 fmt.Printf("type=%T, output=%v\n", s0, s0)  
 s1 := strconv.FormatInt(input, 10)  
 fmt.Printf("type=%T, output=%v\n", s1, s1)  
 s2 := strconv.FormatInt(input, 16)  
 fmt.Printf("type=%T, output=%v\n", s2, s2)  
  
}

输出是

type=int64, output=1134  
type=string, output=10001101110  
type=string, output=1134  
type=string, output=46e  

将整数类型--Int8,Int16,Int32转换为字符串

FormatInt总是返回Int64值。使用int64(int8值)代码,返回的Int64值被转换为字符串类型。同样,Int16(),Int32()也被用来转换为Int16,Int32类型。下面是一个转换其他类型的示例程序,如果FormatInt接受的值不是Int64类型,它就会抛出编译错误--不能在strconv.FormatInt的参数中使用输入1(int8类型)作为int64类型



  
input1 := int8(20)

  
`fmt.Printf("type=%T, output=%v\n", input1, input1)  
s0 := strconv.FormatInt(input1, 10)  
`
```output is  

```go
# command-line-arguments  
.\Test.go:14:25: cannot use input1 (type int8) as type int64 in argument to strconv.FormatInt  

对于其他类型的Int16和Int32也会出现同样的错误,下面是一个错误案例

func main() {  
 input1 := int8(34)  
 input2 := int16(134)  
 input3 := int32(1134)  
 fmt.Printf("type=%T, output=%v\n", input1, input1)  
 s0 := strconv.FormatInt(input1, 10)  
 fmt.Printf("type=%T, output=%v\n", input2, input2)  
 s1 := strconv.FormatInt(input2, 10)  
 fmt.Printf("type=%T, output=%v\n", s1, s1)  
 s2 := strconv.FormatInt(input3, 10)  
 fmt.Printf("type=%T, output=%v\n", s2, s2)  
  
}  

输出是

.\Test.go:14:25: cannot use input1 (type int8) as type int64 in argument to strconv.FormatInt  
.\Test.go:16:25: cannot use input2 (type int16) as type int64 in argument to strconv.FormatInt  
.\Test.go:18:25: cannot use input3 (type int32) as type int64 in argument to strconv.FormatInt  

FormatInt只接受Int64值,我们需要将Int8、Int16、Int32转换为Int64类型。以下是一个完整的例子

package main  
  
import (  
 "fmt"  
 "strconv"  
)  
  
func main() {  
 input1 := int8(34)  
 input2 := int16(134)  
 input3 := int32(1134)  
 fmt.Printf("type=%T, output=%v\n", input1, input1)  
 s0 := strconv.FormatInt(int64(input1), 10)  
 fmt.Printf("type=%T, output=%v\n", s0, s0)  
 s1 := strconv.FormatInt(int64(input2), 10)  
 fmt.Printf("type=%T, output=%v\n", s1, s1)  
 s2 := strconv.FormatInt(int64(input3), 10)  
 fmt.Printf("type=%T, output=%v\n", s2, s2)  
  
}  

输出为

type=int8, output=34  
type=string, output=34  
type=string, output=134  
type=string, output=1134