From 40664f145aef849c0ff31a93c10866c16cb0c054 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Thu, 26 May 2022 23:56:29 +0800 Subject: [PATCH] - init --- .drone.yml | 17 +++++++ .gitignore | 9 ++++ go.mod | 5 ++ go.sum | 2 + godecimal.go | 115 ++++++++++++++++++++++++++++++++++++++++++++++ godecimal_test.go | 16 +++++++ version.go | 3 ++ version_test.go | 7 +++ 8 files changed, 174 insertions(+) create mode 100644 .drone.yml create mode 100644 .gitignore create mode 100644 go.mod create mode 100644 go.sum create mode 100644 godecimal.go create mode 100644 godecimal_test.go create mode 100644 version.go create mode 100644 version_test.go diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..c56c479 --- /dev/null +++ b/.drone.yml @@ -0,0 +1,17 @@ +kind: pipeline +type: docker +name: clone + +steps: + - name: Test + image: golang:1.18 + commands: + - go env -w GO111MODULE=on + - go env -w GOPROXY=https://goproxy.cn,direct + - go test -v ./... + - name: Benchmark + image: golang:1.18 + commands: + - go env -w GO111MODULE=on + - go env -w GOPROXY=https://goproxy.cn,direct + - go test -bench=. -benchmem \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4794692 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +.env +.git +.svn +.idea +.vscode +*.log +goinit.sh +gomod.sh +/vendor/ \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..ec068e7 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module go.dtapp.net/godecimal + +go 1.18 + +require go.dtapp.net/gostring v1.0.3 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..5d8b1b3 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +go.dtapp.net/gostring v1.0.3 h1:KSOq4D77/g5yZN/bqWfZ0kOOaPr/P1240vg03+XdENI= +go.dtapp.net/gostring v1.0.3/go.mod h1:+ggrOvgQDQturi1QGsXEpyRN/ZPoRDaqhMujIk5lrgQ= diff --git a/godecimal.go b/godecimal.go new file mode 100644 index 0000000..fec8758 --- /dev/null +++ b/godecimal.go @@ -0,0 +1,115 @@ +package godecimal + +import ( + "fmt" + "go.dtapp.net/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) +} diff --git a/godecimal_test.go b/godecimal_test.go new file mode 100644 index 0000000..f816c9e --- /dev/null +++ b/godecimal_test.go @@ -0,0 +1,16 @@ +package godecimal + +import ( + "testing" +) + +func TestDecimal(t *testing.T) { + t.Log(Decimal(2.3333)) +} + +func TestRound(t *testing.T) { + t.Log(Round(2.3333, 1)) + t.Log(Round(2.3333, 2)) + t.Log(Round(2.3333, 3)) + t.Log(Round(2.3333, 4)) +} diff --git a/version.go b/version.go new file mode 100644 index 0000000..c0b8aad --- /dev/null +++ b/version.go @@ -0,0 +1,3 @@ +package godecimal + +const Version = "1.0.0" diff --git a/version_test.go b/version_test.go new file mode 100644 index 0000000..750d967 --- /dev/null +++ b/version_test.go @@ -0,0 +1,7 @@ +package godecimal + +import "testing" + +func TestVersion(t *testing.T) { + t.Log(Version) +}