5
golang 处理浮点数的疑问
source link: https://www.v2ex.com/t/894739
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
由于浮点数和整数直接相乘,有丢失精度的问题,查了查资料用了第三方的库 github.com/shopspring/decimal
num2 := 512
num1 := 2.7
d, _ := decimal.NewFromFloat(num1).Mul(decimal.NewFromInt(int64(num2))).Float64()
fmt.Printf("d = %f", d)
测试计算结果是 1382.400024 ,好像也不是准确的数字啊?
update: 好像从数据库取出来的 num1(float 类型)就不精确,难道要用 string 来存储比较好么。。。。
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK