初始
This commit is contained in:
38
api/v1/common/coryCommon/largeNumber.go
Normal file
38
api/v1/common/coryCommon/largeNumber.go
Normal file
@ -0,0 +1,38 @@
|
||||
package coryCommon
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"math/big"
|
||||
)
|
||||
|
||||
// PercentageFunc 百分比计算(精度很高:比如说总数100,完成100,最终得到结果为99.99999999%,那么会直接操作成100)
|
||||
// precision:设置精度
|
||||
// total:总数据量
|
||||
// finish:完成度
|
||||
func PercentageFunc(precision uint, total, finish float64) (consequence float64, err error) {
|
||||
if total == 0 {
|
||||
err = errors.New("总数据不能为初始值")
|
||||
return 0, err
|
||||
}
|
||||
if precision == 0 {
|
||||
err = errors.New("精度不能为初始值")
|
||||
return 0, err
|
||||
}
|
||||
consequence = 0
|
||||
// 定义大浮点数
|
||||
numerator := big.NewFloat(100)
|
||||
denominator := big.NewFloat(total)
|
||||
result := big.NewFloat(finish)
|
||||
// 设置精度
|
||||
//var precision uint = 100 // 设置为你需要的精度
|
||||
numerator.SetPrec(precision)
|
||||
denominator.SetPrec(precision)
|
||||
result.SetPrec(precision)
|
||||
// 计算结果
|
||||
result.Quo(result, denominator)
|
||||
result.Mul(result, numerator)
|
||||
// 截取到两位小数
|
||||
resultRounded, _ := result.Float64()
|
||||
consequence = float64(int(resultRounded*100)) / 100 // 保留两位小数
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user