github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/internal/decimal/README.md (about)

     1  # Decimal package
     2  
     3  Package decimal provides tools for working with YDB's decimal types.
     4  
     5  Decimal types are stored as int128 values inside YDB and represented as 16-byte
     6  arrays in ydb package and as *math/big.Int in ydb/decimal package.
     7  
     8  Note that returned big.Int values are scaled. That is, math operations must be
     9  prepared keeping in mind scaling factor:
    10  
    11  	import (
    12  		"ydb/decimal"
    13  		"math/big"
    14  	)
    15  
    16  	var scaleFactor = big.NewInt(10000000000) // Default scale is 9.
    17  
    18  	func main() {
    19  		x := decimal.FromInt128([16]byte{...})
    20  		x.Add(x, big.NewInt(42)) // Incorrect.
    21  		x.Add(x, scale(42))      // Correct.
    22  	}
    23  
    24  	func scale(n int64) *big.Int {
    25  		x := big.NewInt(n)
    26  		return x.Mul(x, scaleFactor)
    27  	}