Golang strconv.FormatInt
最后修改于 2025 年 4 月 20 日
本教程解释了如何在 Go 中使用 strconv.FormatInt 函数。我们将通过实际示例介绍整数到字符串转换的基础知识。
strconv.FormatInt 函数将整数转换为字符串。它是 Go 中用于格式化数字输出最常用的函数之一。
FormatInt 允许指定转换的基数(2-36)。该函数以指定基数返回整数的字符串表示形式。
基本的 strconv.FormatInt 示例
strconv.FormatInt 最简单的用法是将整数转换为十进制字符串。这里我们展示了基本的转换。
package main
import (
"fmt"
"strconv"
)
func main() {
num := int64(42)
str := strconv.FormatInt(num, 10)
fmt.Printf("Integer %d converted to string '%s'\n", num, str)
}
我们将整数 42 转换为十进制字符串。该函数接受一个 int64 值,并返回其在指定基数下的字符串表示形式。
转换为不同的基数
strconv.FormatInt 支持转换为各种基数。此示例显示了转换为二进制、八进制、十进制和十六进制。
package main
import (
"fmt"
"strconv"
)
func main() {
num := int64(42)
fmt.Println("Binary:", strconv.FormatInt(num, 2))
fmt.Println("Octal:", strconv.FormatInt(num, 8))
fmt.Println("Decimal:", strconv.FormatInt(num, 10))
fmt.Println("Hexadecimal:", strconv.FormatInt(num, 16))
}
我们将相同的数字转换为不同的基数。基数 2 是二进制,8 是八进制,10 是十进制,16 是十六进制表示。
处理负数
strconv.FormatInt 可以正确处理负数。此示例演示了负值的转换。
package main
import (
"fmt"
"strconv"
)
func main() {
num := int64(-42)
fmt.Println("Decimal:", strconv.FormatInt(num, 10))
fmt.Println("Hexadecimal:", strconv.FormatInt(num, 16))
fmt.Println("Binary:", strconv.FormatInt(num, 2))
}
负数将转换为带前导负号。基数转换的工作方式与正数相同。
FormatInt 与 Itoa 的比较
strconv.Itoa 是十进制转换的更简单替代方法。此示例比较了 FormatInt 和 Itoa。
package main
import (
"fmt"
"strconv"
)
func main() {
num := int64(42)
// Using FormatInt for base 10
str1 := strconv.FormatInt(num, 10)
fmt.Println("FormatInt:", str1)
// Using Itoa (only for base 10)
str2 := strconv.Itoa(int(num))
fmt.Println("Itoa:", str2)
}
Itoa 对于 int 值的十进制转换更方便。当需要其他基数或处理 int64 时,需要 FormatInt。
性能注意事项
对于性能关键型代码,了解转换成本很重要。此示例将 FormatInt 与其他方法进行基准测试。
package main
import (
"fmt"
"strconv"
"time"
)
func main() {
const iterations = 1000000
num := int64(12345)
// Benchmark FormatInt
start := time.Now()
for i := 0; i < iterations; i++ {
strconv.FormatInt(num, 10)
}
fmt.Println("FormatInt duration:", time.Since(start))
// Benchmark fmt.Sprintf
start = time.Now()
for i := 0; i < iterations; i++ {
fmt.Sprintf("%d", num)
}
fmt.Println("Sprintf duration:", time.Since(start))
}
对于整数到字符串的转换,FormatInt 比 fmt.Sprintf 快得多。当性能很重要时,请使用 FormatInt。
替代方法:fmt.Sprintf
fmt.Sprintf 提供了另一种将整数格式化为字符串的方法。此示例将其与 FormatInt 进行比较。
package main
import (
"fmt"
"strconv"
)
func main() {
num := int64(42)
// Using FormatInt
str1 := strconv.FormatInt(num, 10)
fmt.Println("FormatInt result:", str1)
// Using Sprintf
str2 := fmt.Sprintf("%d", num)
fmt.Println("Sprintf result:", str2)
}
Sprintf 比 FormatInt 更灵活但速度更慢。对于简单转换,请使用 FormatInt;当您需要复杂格式时,请使用 Sprintf。
实际示例:数字格式化
此实际示例演示了如何使用 FormatInt 创建带有填充和不同基数的格式化数字字符串。
package main
import (
"fmt"
"strconv"
"strings"
)
func main() {
numbers := []int64{7, 42, 255, 1024, 65535}
for _, num := range numbers {
// Format with leading zeros
dec := strconv.FormatInt(num, 10)
dec = fmt.Sprintf("%06s", dec)
// Format as hexadecimal
hex := strconv.FormatInt(num, 16)
hex = strings.ToUpper(hex)
hex = fmt.Sprintf("0x%04s", hex)
fmt.Printf("%d → Dec: %s, Hex: %s\n", num, dec, hex)
}
}
我们将数字格式化为带前导零和十六进制前缀。FormatInt 提供基数转换,而 fmt.Sprintf 处理填充。
来源
本教程通过各种场景下整数到字符串转换的实际示例,介绍了 Go 中的 strconv.FormatInt 函数。