目录
无论是 32位系统还是 64 位系统,都支持 float64
string 转换为 float
package main
import (
"fmt"
"strconv"
)
func main() {
input := "3.14"
f_input, _ := strconv.ParseFloat(input, 64)
fmt.Printf("%f - %T", f_input, f_input)
}
执行结果
> go run main.go
3.140000 - float64
int 转化为 float
score := 100
f_score := float64(score)
fmt.Printf("%f - %T\n", f_score, f_score)
> 100.000000 - float64
转换为 string,并保留3位小数
s_score := fmt.Sprintf("%.3f", f_score)
注意,这样保留3位小数会自动四舍五入。
无论是 32位系统还是 64 位系统,都支持 float64
ubuntu 查看系统是 32 位还是 64 位
> uname -a
Linux 509B65C8YW2THMJ 4.4.0-18362-Microsoft #1-Microsoft Mon Mar 18 12:02:00 PST 2019 x86_64 x86_64 x86_64 GNU/Linux
因为32位只是寄存器的一次处理的位数。配合不同的算法,多大的数字都可以处理。
本文介绍了如何在Go语言中将字符串转换为浮点数(float64),如何将整数(int)转换为浮点数,以及如何将浮点数转换为字符串并保留三位小数。此外还提及了在不同系统架构下float64类型的适用性。
1074

被折叠的 条评论
为什么被折叠?



