Home>Article>Backend Development> How to solve the problem of loss of accuracy of floating point numbers in Golang extension package

How to solve the problem of loss of accuracy of floating point numbers in Golang extension package

藏色散人
藏色散人 forward
2021-03-09 13:46:14 3089browse

The following is thego languagetutorial column to introduce to you theGolangextension package solution to the problem of floating point precision loss, Hope it helps those in need!

How to solve the problem of loss of accuracy of floating point numbers in Golang extension package

#PS: Today, when I was working on a project for precision processing, I had a precision loss problem. Here I would like to share with you the expansion package solution. Note: This problem may occur, but not necessarily

① Install the expansion pack
go get github.com/shopspring/decimal
② How to use
//首先设置保留位数 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!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete