0
点赞
收藏
分享

微信扫一扫

golang float转string后去除末尾的0

东方小不点 2022-04-29 阅读 108
golang

go语言中,float转成string后,末尾会有多余的0,例如1.20000。显示效果不友好,可以使用以下方法去除末尾的0:

floatValue := 123.222
result := strconv.FormatFloat(floatValue, 'f', 4, 64)
// 去除result末尾的0
for strings.HasSuffix(result, "0") {
	result = strings.TrimSuffix(result, "0")
}
if strings.HasSuffix(result, ".") {
	result = strings.TrimSuffix(result, ".")
}
举报

相关推荐

0 条评论