parent
9eefb9eef1
commit
fd91f6808d
@ -0,0 +1,15 @@
|
||||
package godecimal
|
||||
|
||||
import (
|
||||
"math/big"
|
||||
)
|
||||
|
||||
type Decimal struct {
|
||||
floatValue *big.Float
|
||||
}
|
||||
|
||||
func New() Decimal {
|
||||
return Decimal{
|
||||
floatValue: new(big.Float),
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package godecimal
|
||||
|
||||
// NewString 从字符串创建
|
||||
func NewString(s string) Decimal {
|
||||
d := New()
|
||||
d.floatValue.SetString(s)
|
||||
return d
|
||||
}
|
||||
|
||||
// NewFloat 从浮点数创建
|
||||
func NewFloat(f float64) Decimal {
|
||||
d := New()
|
||||
d.floatValue.SetFloat64(f)
|
||||
return d
|
||||
}
|
||||
|
||||
// NewInt 从整数创建
|
||||
func NewInt(i int64) Decimal {
|
||||
d := New()
|
||||
d.floatValue.SetInt64(i)
|
||||
return d
|
||||
}
|
||||
|
||||
// NewUint 从无符合整数创建
|
||||
func NewUint(i uint64) Decimal {
|
||||
d := New()
|
||||
d.floatValue.SetUint64(i)
|
||||
return d
|
||||
}
|
@ -0,0 +1,113 @@
|
||||
package godecimal
|
||||
|
||||
// Add 加 (d+d2)
|
||||
func (d Decimal) Add(d2 Decimal) Decimal {
|
||||
mul := New()
|
||||
mul.floatValue.Add(d.floatValue, d2.floatValue)
|
||||
return mul
|
||||
}
|
||||
|
||||
// AddFloat 加
|
||||
func (d Decimal) AddFloat(d2 float64) Decimal {
|
||||
mul := New()
|
||||
mul.floatValue.Add(d.floatValue, NewFloat(d2).floatValue)
|
||||
return mul
|
||||
}
|
||||
|
||||
// AddInt 加
|
||||
func (d Decimal) AddInt(d2 int64) Decimal {
|
||||
mul := New()
|
||||
mul.floatValue.Add(d.floatValue, NewInt(d2).floatValue)
|
||||
return mul
|
||||
}
|
||||
|
||||
// AddString 加
|
||||
func (d Decimal) AddString(d2 string) Decimal {
|
||||
mul := New()
|
||||
mul.floatValue.Add(d.floatValue, NewString(d2).floatValue)
|
||||
return mul
|
||||
}
|
||||
|
||||
// Sub 减 (d-d2)
|
||||
func (d Decimal) Sub(d2 Decimal) Decimal {
|
||||
mul := New()
|
||||
mul.floatValue.Sub(d.floatValue, d2.floatValue)
|
||||
return mul
|
||||
}
|
||||
|
||||
// SubFloat 减
|
||||
func (d Decimal) SubFloat(d2 float64) Decimal {
|
||||
mul := New()
|
||||
mul.floatValue.Sub(d.floatValue, NewFloat(d2).floatValue)
|
||||
return mul
|
||||
}
|
||||
|
||||
// SubInt 减
|
||||
func (d Decimal) SubInt(d2 int64) Decimal {
|
||||
mul := New()
|
||||
mul.floatValue.Sub(d.floatValue, NewInt(d2).floatValue)
|
||||
return mul
|
||||
}
|
||||
|
||||
// SubString 减
|
||||
func (d Decimal) SubString(d2 string) Decimal {
|
||||
mul := New()
|
||||
mul.floatValue.Sub(d.floatValue, NewString(d2).floatValue)
|
||||
return mul
|
||||
}
|
||||
|
||||
// Mul 乘 (d*d2)
|
||||
func (d Decimal) Mul(d2 Decimal) Decimal {
|
||||
mul := New()
|
||||
mul.floatValue.Mul(d.floatValue, d2.floatValue)
|
||||
return mul
|
||||
}
|
||||
|
||||
// MulFloat 乘
|
||||
func (d Decimal) MulFloat(d2 float64) Decimal {
|
||||
mul := New()
|
||||
mul.floatValue.Mul(d.floatValue, NewFloat(d2).floatValue)
|
||||
return mul
|
||||
}
|
||||
|
||||
// MulInt 乘
|
||||
func (d Decimal) MulInt(d2 int64) Decimal {
|
||||
mul := New()
|
||||
mul.floatValue.Mul(d.floatValue, NewInt(d2).floatValue)
|
||||
return mul
|
||||
}
|
||||
|
||||
// MulString 乘
|
||||
func (d Decimal) MulString(d2 string) Decimal {
|
||||
mul := New()
|
||||
mul.floatValue.Mul(d.floatValue, NewString(d2).floatValue)
|
||||
return mul
|
||||
}
|
||||
|
||||
// Quo 除 (d/d2)
|
||||
func (d Decimal) Quo(d2 Decimal) Decimal {
|
||||
mul := New()
|
||||
mul.floatValue.Quo(d.floatValue, d2.floatValue)
|
||||
return mul
|
||||
}
|
||||
|
||||
// QuoFloat 除
|
||||
func (d Decimal) QuoFloat(d2 float64) Decimal {
|
||||
mul := New()
|
||||
mul.floatValue.Quo(d.floatValue, NewFloat(d2).floatValue)
|
||||
return mul
|
||||
}
|
||||
|
||||
// QuoInt 除
|
||||
func (d Decimal) QuoInt(d2 int64) Decimal {
|
||||
mul := New()
|
||||
mul.floatValue.Quo(d.floatValue, NewInt(d2).floatValue)
|
||||
return mul
|
||||
}
|
||||
|
||||
// QuoString 除
|
||||
func (d Decimal) QuoString(d2 string) Decimal {
|
||||
mul := New()
|
||||
mul.floatValue.Quo(d.floatValue, NewString(d2).floatValue)
|
||||
return mul
|
||||
}
|
@ -0,0 +1,81 @@
|
||||
package godecimal
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"math/big"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// String 输出 string
|
||||
func (d Decimal) String() string {
|
||||
return d.floatValue.String()
|
||||
}
|
||||
|
||||
// Int64 输出 int64
|
||||
func (d Decimal) Int64() int64 {
|
||||
i64, _ := d.floatValue.Int64()
|
||||
return i64
|
||||
}
|
||||
|
||||
// Float64 输出 float64
|
||||
func (d Decimal) Float64() float64 {
|
||||
rat, _ := new(big.Rat).SetString(d.String())
|
||||
f, _ := rat.Float64()
|
||||
return f
|
||||
}
|
||||
|
||||
// MoneyFloat64 货币 float64
|
||||
func (d Decimal) MoneyFloat64() float64 {
|
||||
rat, _ := new(big.Rat).SetString(d.floatValue.Text('f', 2))
|
||||
f, _ := rat.Float64()
|
||||
return f
|
||||
}
|
||||
|
||||
// Float64Point 输出float64带小数点
|
||||
func (d Decimal) Float64Point(p int) float64 {
|
||||
rat, _ := new(big.Rat).SetString(d.floatValue.Text('f', p))
|
||||
f, _ := rat.Float64()
|
||||
return f
|
||||
}
|
||||
|
||||
// Float64PointAdaptive 输出float64带小数点(自适应)
|
||||
func (d Decimal) Float64PointAdaptive(maxP int) float64 {
|
||||
f, _ := d.floatValue.Float64()
|
||||
if maxP > 0 {
|
||||
pL := d.pointLength(f)
|
||||
if pL > maxP {
|
||||
rat, _ := new(big.Rat).SetString(d.floatValue.Text('f', maxP))
|
||||
f2, _ := rat.Float64()
|
||||
return f2
|
||||
} else {
|
||||
return f
|
||||
}
|
||||
} else {
|
||||
return f
|
||||
}
|
||||
}
|
||||
|
||||
func (Decimal) pointLength(a any) int {
|
||||
tmp := strings.Split(fmt.Sprint(a), ".")
|
||||
if len(tmp) <= 1 {
|
||||
return 0
|
||||
}
|
||||
return len(tmp[1])
|
||||
}
|
||||
|
||||
// IsInteger 是否为整数
|
||||
func (d Decimal) IsInteger(d2 float64) bool {
|
||||
if d2 > 0 {
|
||||
f3 := NewFloat(d.Float64()).QuoFloat(NewFloat(d2).Float64()).Float64()
|
||||
if f3 == math.Trunc(f3) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
f3 := NewFloat(d.Float64()).Float64()
|
||||
if f3 == math.Trunc(f3) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package godecimal
|
||||
|
||||
// Float64Add 加 (f1+f2)
|
||||
func Float64Add(f1, f2 float64) float64 {
|
||||
return NewFloat(f1).Add(NewFloat(f2)).Float64()
|
||||
}
|
||||
|
||||
// Float64Sub 减 (f1-f2)
|
||||
func Float64Sub(f1, f2 float64) float64 {
|
||||
return NewFloat(f1).Sub(NewFloat(f2)).Float64()
|
||||
}
|
||||
|
||||
// Float64Mul 乘 (f1*f2)
|
||||
func Float64Mul(f1, f2 float64) float64 {
|
||||
return NewFloat(f1).Mul(NewFloat(f2)).Float64()
|
||||
}
|
||||
|
||||
// Float64Quo 除 (f1/f2)
|
||||
func Float64Quo(f1, f2 float64) float64 {
|
||||
return NewFloat(f1).Quo(NewFloat(f2)).Float64()
|
||||
}
|
@ -1,115 +0,0 @@
|
||||
package godecimal
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/dtapps/go-library/utils/gostring"
|
||||
"math"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// Decimal 四舍五入
|
||||
func Decimal(value float64) float64 {
|
||||
value, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", value), 64)
|
||||
return value
|
||||
}
|
||||
|
||||
// Round 四舍五入
|
||||
func Round(f float64, n int) float64 {
|
||||
n10 := math.Pow10(n)
|
||||
return math.Trunc((f+0.5/n10)*n10) / n10
|
||||
}
|
||||
|
||||
// RoundYInt64 四舍五入
|
||||
func RoundYInt64(y int64, n int) float64 {
|
||||
return Round(float64(y/100), n)
|
||||
}
|
||||
|
||||
// RoundYString 四舍五入
|
||||
func RoundYString(y string, n int) float64 {
|
||||
return Round(gostring.ToFloat64(y)/100, n)
|
||||
}
|
||||
|
||||
// Multiply 相乘
|
||||
func Multiply(y, x float64) float64 {
|
||||
return Round(y*x, 2)
|
||||
}
|
||||
|
||||
// PddCouponAmount 优惠券金额
|
||||
func PddCouponAmount(y int64) float64 {
|
||||
return Round(float64(y)/100, 2)
|
||||
}
|
||||
|
||||
// PddCouponProportion 拼多多佣金比率
|
||||
func PddCouponProportion(y int64) float64 {
|
||||
return Round(float64(y)/10, 2)
|
||||
}
|
||||
|
||||
// PddGoodsOriginalPrice 拼多多商品原价
|
||||
func PddGoodsOriginalPrice(y int64) float64 {
|
||||
return Round(float64(y)/100, 2)
|
||||
}
|
||||
|
||||
// PddGoodsPrice 拼多多商品券后价
|
||||
func PddGoodsPrice(y, x float64) float64 {
|
||||
return Round(y-x, 2)
|
||||
}
|
||||
|
||||
// PddCommission 拼多多佣金
|
||||
func PddCommission(y, x float64) float64 {
|
||||
return Round((y*x)/100, 2)
|
||||
}
|
||||
|
||||
// TbCouponAmount 淘宝优惠券金额
|
||||
func TbCouponAmount(y int64) float64 {
|
||||
return Round(float64(y), 2)
|
||||
}
|
||||
|
||||
// TbCouponProportion 淘宝佣金比率
|
||||
func TbCouponProportion(y string) float64 {
|
||||
return Round(gostring.ToFloat64(y)/100, 2)
|
||||
}
|
||||
|
||||
// TbGoodsOriginalPrice 淘宝商品原价
|
||||
func TbGoodsOriginalPrice(y string) float64 {
|
||||
return Round(gostring.ToFloat64(y), 2)
|
||||
}
|
||||
|
||||
// TbGoodsPrice 淘宝商品券后价
|
||||
func TbGoodsPrice(y, x float64) float64 {
|
||||
return Round(y-x, 2)
|
||||
}
|
||||
|
||||
// TbCommission 淘宝佣金
|
||||
func TbCommission(y, x float64) float64 {
|
||||
return Round((y*x)/100, 2)
|
||||
}
|
||||
|
||||
// WmCouponAmount 小商店优惠券金额
|
||||
func WmCouponAmount(y string) float64 {
|
||||
return Round(gostring.ToFloat64(y)/100, 2)
|
||||
}
|
||||
|
||||
// WmCouponProportion 小商店佣金比率
|
||||
func WmCouponProportion(y int64) float64 {
|
||||
return Round(float64(y)/100, 2)
|
||||
}
|
||||
|
||||
// WmCommission 小商店佣金
|
||||
func WmCommission(y int64) float64 {
|
||||
return Round(float64(y)/100, 2)
|
||||
}
|
||||
|
||||
// WmGoodsOriginalPrice 小商店商品原价
|
||||
func WmGoodsOriginalPrice(y int64) float64 {
|
||||
return Round(float64(y)/100, 2)
|
||||
}
|
||||
|
||||
// WmGoodsPrice 小商店商品券后价
|
||||
func WmGoodsPrice(y int64) float64 {
|
||||
return Round(float64(y)/100, 2)
|
||||
}
|
||||
|
||||
// JdCommission 京东佣金
|
||||
func JdCommission(y, x float64) float64 {
|
||||
return Round((y*x)/100, 2)
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package godecimal
|
||||
|
||||
import "math"
|
||||
|
||||
// Abs 取绝对值
|
||||
func Abs(x float64) float64 {
|
||||
return math.Abs(x)
|
||||
}
|
||||
|
||||
// Floor 向下取整
|
||||
func Floor(x float64) float64 {
|
||||
return math.Floor(x)
|
||||
}
|
||||
|
||||
// Ceil 向上取整
|
||||
func Ceil(x float64) float64 {
|
||||
return math.Ceil(x)
|
||||
}
|
||||
|
||||
// Round 就近取整
|
||||
func Round(x float64) float64 {
|
||||
return math.Round(x)
|
||||
}
|
||||
|
||||
// RoundPoint 就近取整并保留小数点
|
||||
func RoundPoint(x float64) float64 {
|
||||
return math.Round(x*100) / 100
|
||||
}
|
||||
|
||||
// Max 取较大值
|
||||
func Max(x, y float64) float64 {
|
||||
return math.Max(x, y)
|
||||
}
|
||||
|
||||
// Min 取较小值
|
||||
func Min(x, y float64) float64 {
|
||||
return math.Min(x, y)
|
||||
}
|
Loading…
Reference in new issue