SoFunction
Updated on 2025-03-05

Detailed explanation of the usage of decimal in go language

decimal is a library created to solve the problem of accuracy loss in floating point number calculation in Golang. Using the decimal library, we can avoid the problem of accuracy loss in using floating point numbers in Go.

Github address:/shopspring/decimal

1. Cases with lost precision

func TestFloat(t *) {
    a := 1100.1
    b := a * 100
    (b) // should be: 110010 output: 110009.99999999999
}

The above accuracy is lost. Although we see 1100.1 in the floating point number, it is infinitely close to 1 for the calculation of floating point number, but it is not equal to 1.

When we need to calculate the float data, we can use a third-party decimal package to solve this problem.

2. Application scenarios of decimal

The application scenario of decimal mainly occurs when adding, subtracting, multiplication and division operations on float floating point numbers, especially for banking and financial businesses. If the accuracy is lost, the losses on a transaction can be negligible, but when the scale of the transaction reaches tens of millions, billions or even billions, the losses at this time will be scary.

3. Use decimal

The first step to using decimal is to introduce this package, decimal. In the official description, the function description of this package is as follows:

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.

Decimal number with any precision fixed point in go

Note: The decimal library "can only" represent numbers with up to 2^31 digits after the decimal point.

The 2^31 digits after the decimal point are sufficient for most project accuracy requirements. In short, decimal can solve most of our floating-point accuracy calculation scenarios.

For the case with the above accuracy loss, we can get the correct result after we introduce the decimal package.

func TestDecimalOne(t *){
    a := 1100.1
    b := 100
    d := (a)
    result := ((int64(b)))
    (result) // should be: 110010, output: 110010
}

The accuracy of the calculation result is not lost, and the calculation result is correct.

4. Decimal Other Practical Scenarios

When using decimal, remember that floating-point number calculations must be initialized through decimal, otherwise it will still lead to loss of accuracy. Why do you say that? Take a look at the following example and you will understand.

import (
    "fmt"
    "/shopspring/decimal"
    "testing"
)

func TestDecimal(t *) {
    x := 0.28
    // this will cause the multi result has error
    // output should be 28, but actually 28.000000000000004(error)
    errorMul := (x * 100).String()
}

The above case is because 100 is not initialized using decimal, so the accuracy of the final calculation is expanded. The correct way to deal with it is as follows:

import (
    "fmt"
    "/shopspring/decimal"
    "testing"
)

func TestDecimal(t *) {
    x := 0.28
    // the correct operate number multi is use the decimal Mul method.
    correctMul := (x).Mul((100)).String()
    (correctMul) // output: 28
}

4.1 Get the integer part of the result

Use IntPart to get the integer part of the floating point calculation result.

func TestDecimalTwo(t *){
    a := 0.01234
    b := 100
    // 0.01234 * 100 = 1.234, int part=1, output=1
    ((a).Mul((int64(b))).IntPart())
}

4.2 Fill after decimal point

Use IntPart to fill the calculated data with zeros.

func TestDecimalThree(t *){
    a := 0.012
    b := 100
    x := (a).Mul((int64(b)))
    // The number of digits after the decimal point is filled, 0.012*100=1.2, fill 3 digits, because there are already one, fill two 0, 1.200    ((3))
}

4.3 Compare the size of the number

Comparison of floating point numbers decimal provides some useful function methods, and some of them are listed here.

import (
    "fmt"
    "/shopspring/decimal"
    "testing"
)

func TestDecimalFour(t *) {
    a := (-1.11)
    b := (3)
    c, _ := ("2.023")
    // Is it a negative number    (())
    // Take the absolute value    (())
    // Compare whether it is equal    ((b))
    // is smaller than    ((b))
    // is less than or equal to    ((b))
    // More than or equal to    ((a))
    // Is it 0    (())
}

5 Summary

decimal provides great convenience for the calculation of floating-point numbers, allowing us to use floating-point numbers to perform size calculations without worrying about accuracy loss, especially for the financial industry, loss of accuracy causes capital losses, which is a very serious production accident.

We have some basic understanding of decimal. When we have scenarios in our work that we need to use floating-point calculations, we can directly use decimal to help us quickly improve the calculation logic.

This is the end of this article about the detailed explanation of the usage of decimal in go language. For more relevant content on the usage of decimal in go language, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!