github.com/spi-ca/misc@v1.0.1/nums/big.go (about)

     1  package nums
     2  
     3  import (
     4  	"github.com/ericlagergren/decimal"
     5  )
     6  
     7  // BigSeperateFractionalPotions decompose a decimal number with a sign, integer, and fractional parts.
     8  func BigSeperateFractionalPotions(src *decimal.Big) (sign bool, integer, fractional uint64, fractionalLength uint8) {
     9  	var num, integerPart decimal.Big
    10  	num.Copy(src)
    11  	num.Context.RoundingMode = decimal.ToZero
    12  	sign = num.Signbit()
    13  	integer, _ = num.Abs(&num).Uint64()
    14  	integerPart.SetUint64(integer)
    15  	scale := num.Sub(&num, &integerPart).Quantize(6).Reduce().Scale()
    16  	fractional, _ = num.Mul(&num, decimal.New(1, -scale)).Reduce().Uint64()
    17  	fractionalLength = uint8(scale)
    18  	return
    19  }
    20  
    21  // BigMergeFractionalPotions compose a decimal number with a sign, integer, and fractional parts.
    22  func BigMergeFractionalPotions(sign bool, integer, fractional uint64, fractionalLength uint8) (dst *decimal.Big) {
    23  	var num, frac decimal.Big
    24  	num.SetUint64(integer)
    25  	frac.SetUint64(fractional)
    26  	dst = &num
    27  
    28  	num.Mul(&num, decimal.New(1, -int(fractionalLength))).Add(&num, &frac).SetScale(int(fractionalLength))
    29  
    30  	if sign {
    31  		num.Neg(&num)
    32  	}
    33  	num.Reduce()
    34  	return
    35  }