gitee.com/quant1x/gox@v1.7.6/num/decimals.go (about)

     1  package num
     2  
     3  import "math"
     4  
     5  // Decimal 保留小数点四舍五入
     6  func Decimal(value float64, digits ...int) float64 {
     7  	defaultDigits := 2
     8  	if len(digits) > 0 {
     9  		defaultDigits = digits[0]
    10  	}
    11  	n10 := math.Pow10(defaultDigits)
    12  	return math.Trunc((value+0.5/n10)*n10) / n10
    13  }
    14  
    15  // NetChangeRate 净增长率, 去掉百分比
    16  func NetChangeRate[B Number, C Number](baseValue B, currentValue C) (changeRate float64) {
    17  	changeRate = ChangeRate(baseValue, currentValue)
    18  	changeRate = (changeRate - 1.00) * 100.00
    19  	return changeRate
    20  }
    21  
    22  // ChangeRate 增长率
    23  func ChangeRate[B Number, C Number](baseValue B, currentValue C) (changeRate float64) {
    24  	return float64(currentValue) / float64(baseValue)
    25  }