You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
866 B
Go

package decimal
import (
"github.com/shopspring/decimal"
)
// Div 除以
func Div(numerator, denominator int64) decimal.Decimal {
num, den := decimal.NewFromInt(numerator), decimal.NewFromInt(denominator)
return num.Div(den)
}
// Percent 百分额
func Percent(numerator, denominator int64) decimal.Decimal {
return Div(numerator, denominator).Mul(decimal.NewFromFloat(100))
}
// Chain 环比
func Chain(numerator, denominator int64) decimal.Decimal {
num, den := decimal.NewFromInt(numerator), decimal.NewFromInt(denominator)
return num.Sub(den).Div(den)
}
// DenZero 分母0处理
func DenZero(numerator, denominator, max int64) (decimal.Decimal, bool) {
if denominator != 0 {
return decimal.Decimal{}, false
}
if numerator == 0 {
return decimal.Zero, true
}
if numerator > max {
numerator = max
}
return decimal.NewFromInt(max), true
}