可以使用 fmt.Sprintf 或者 strconv.FormatFloat 将 float 转换为 string。

使用 fmt.Sprintf

f := 3.1415926
s := fmt.Sprintf('%f', f)

使用 strconv.FormatFloat

f := 3.1415926
s := strconv.FormatFloat(f, 'f', -1, 64)

其中,第一个参数是要转换的 float,第二个参数是格式化选项,'f' 表示浮点数,第三个参数是小数点后保留的位数,-1 表示不限制,第四个参数是转换的 float 类型,64 表示 float64。

Golang 浮点数 (float) 转字符串 (string) 详解

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

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