Preface
When we are involved in numerical calculations in our project, if we use golang's operator directly, it will cause accuracy loss, for example:
a := 1136.1 b := a * 100 (b) // The correct result should be 113610 but the output is 113609.9999999999999 c := 1.7 (a - c) // The correct result should be 1134.4 but the output is 1134.3999999999999999(b - c) // The correct result should be 113608.3, but the output is 113608.2999999999999
So how to avoid this situation? There is no corresponding package for precision operations in golang. Here we need to use a third-party decimal package.
1. What is the Decimal library?
It is a package provided by a third party to avoid accuracy loss when numerical calculation of Go programs. It quotes the official description:
Arbitrary-precision fixed-point decimal numbers in go.
Note: Decimal library can "only" represent numbers with a maximum of 2^31 digits after the decimal point.
Note that there is a Note here: The Decimal library "can only" represent numbers with up to 2^31 digits after the decimal point. Of course, this is enough for most projects.
2. Use steps
1.Introduce the library
Download package:
go get /shopspring/decimal
Packages are introduced in the go code:
import "/shopspring/decimal"
Use of the library
After using decimal, the code in the above example should be written like this:
package main import ( "fmt" "/shopspring/decimal" ) func main() { a := (1136.1) b := ((100)) (b) // Correct output 113610 c := (1.7) ((c)) // Correct output 1134.4 ((c)) // Correct output 113608.3}
Then, you will become easy to calculate various numbers:
package main import ( "fmt" "/shopspring/decimal" ) func main() { a := (1.52) b := (0.02) // Addition, subtraction, multiplication and division operations c := (b) // 1.52 + 0.02 = 1.54 d := (b) // 1.52 - 0.02 = 1.5 e := (b) // 1.52 * 0.02 = 0.0304 f := (b) // 1.52 / 0.02 = 76 (a, b, c, d, e, f) // For the processing of retaining decimals pi := (3.1415926535897932384626) pi1 := (3) // Round 3 decimal places for pi value (pi1) // 3.142 pi2 := (3) // Keep 3 decimal places for pi value and discard it directly (pi2) // 3.141 }
You can also easily convert numeric variable types:
var a float64 var b = "69.77" var c int64 d, err := (b) if err != nil { (()) } a, _ = d.Float64() (a) // float64 69.77 c = () // Skip the decimals and round them(c) // int64 69 b = (c).String() (b) // string 69 c = (0).IntPart() // No decimal rounding is retained(c) // int64 70
Here are some common methods:
n1 := (-1.23) n2 := (3) n3, _ := ("0") () // Take the absolute value(n2) // Is n1 equal to n2(n2) // Is n1 less than n2(n2) // Is n1 less than or equal to n2(n2) // Is n1 greater than n2(n2) // Is n1 greater than or equal to n2() // Is n3 0
Summarize
The Decimal library will provide great convenience and security for numerical calculations in our projects.
The last point to note is that all variable data types used in the Decimal library are, and can also be used as data types when declaring variables, so remember to convert them to the required data type when the variable is assigned at the end.
package main import ( "fmt" "/shopspring/decimal" "reflect" ) func main() { n1 := (3.14) var n2 string var n3 float64 var n4 int64 var n5 n2 = () n3, _ = n1.Float64() n4 = () ("n1 = %v, type = %v\n", n1, (n1).String()) // n1 = 3.14, type = ("n2 = %v, type = %v\n", n2, (n2).String()) // n2 = 3.14, type = string ("n3 = %v, type = %v\n", n3, (n3).String()) // n3 = 3.14, type = float64 ("n4 = %v, type = %v\n", n4, (n4).String()) // n4 = 3, type = int64 ("n5 = %v, type = %v\n", n5, (n5).String()) // n5 = 0, type = }
This is the article about Golang using the Decimal library to avoid accuracy loss in operations. For more related content on Golang to avoid operation accuracy loss, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!