The following is the go language tutorial column to introduce to you the Golang extension package solution to the problem of floating point precision loss, Hope it helps those in need!
go get github.com/shopspring/decimal
//首先设置保留位数 decimal.DivisionPrecision = 2 // 四舍五入保留两位小数 // 进行相加number1和number2 sum := decimal.NewFromFloat(number1).Add(decimal.NewFromFloat(number2)) // 返回的类型为decimal.decimal类型,我们在进行转换为float64就好了 newSum, _ := sum.Float64()
The above is the detailed content of How to solve the problem of loss of accuracy of floating point numbers in Golang extension package. For more information, please follow other related articles on the PHP Chinese website!