Home > Backend Development > Golang > 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

藏色散人
Release: 2021-03-09 13:46:14
forward
3220 people have browsed it

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!

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
Copy after login
② How to use
//首先设置保留位数
decimal.DivisionPrecision = 2  // 四舍五入保留两位小数
// 进行相加number1和number2
sum := decimal.NewFromFloat(number1).Add(decimal.NewFromFloat(number2))
// 返回的类型为decimal.decimal类型,我们在进行转换为float64就好了
newSum, _ := sum.Float64()
Copy after login

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!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template