在Go语言中,可以使用strconv包来实现数字转换为字符串。

package main

import (
	"fmt"
	"strconv"
)

func main() {
	num := 123

	// 将整数转换为字符串
	str := strconv.Itoa(num)
	fmt.Println(str)

	fnum := 3.14

	// 将浮点数转换为字符串
	fstr := strconv.FormatFloat(fnum, 'f', -1, 64)
	fmt.Println(fstr)
}

输出结果为:

123
3.14

在这个示例中,我们使用了strconv.Itoa()函数将整数num转换为字符串。对于浮点数,我们使用了strconv.FormatFloat()函数来实现转换,其中'f'参数指定了浮点数的格式,-1参数表示自动选择最短的表示方式,64参数表示使用float64类型。

golang 数字转string

原文地址: https://www.cveoy.top/t/topic/iKD0 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录