Files
zmkgC/api/v1/common/coryCommon/largeNumber.go
2025-07-07 20:11:59 +08:00

39 lines
1.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
}