Compare commits
14 Commits
Author | SHA1 | Date |
---|---|---|
dtapps | cb4c11ed29 | 5 months ago |
李光春 | 537950e68f | 2 years ago |
李光春 | 3077d5fe77 | 2 years ago |
李光春 | 47ed2dee63 | 2 years ago |
李光春 | d876f75d9f | 2 years ago |
李光春 | 56bdfc6a73 | 2 years ago |
李光春 | e3724e56fe | 2 years ago |
李光春 | 58985c9504 | 2 years ago |
李光春 | 243be10d14 | 2 years ago |
李光春 | 98ebadd42c | 2 years ago |
李光春 | 6ae23a903b | 2 years ago |
李光春 | 823c8c33d1 | 2 years ago |
李光春 | 3fda109580 | 2 years ago |
李光春 | cf80d21881 | 2 years ago |
@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2018 李光春
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
@ -0,0 +1,36 @@
|
||||
<h1>
|
||||
<a href="https://www.dtapp.net/">Golang Decimal</a>
|
||||
</h1>
|
||||
|
||||
📦 Golang 小数点处理
|
||||
|
||||
[comment]: <> (go)
|
||||
[![godoc](https://pkg.go.dev/badge/go.dtapp.net/godecimal?status.svg)](https://pkg.go.dev/go.dtapp.net/godecimal)
|
||||
[![goproxy.cn](https://goproxy.cn/stats/go.dtapp.net/godecimal/badges/download-count.svg)](https://goproxy.cn/stats/go.dtapp.net/godecimal)
|
||||
[![goreportcard.com](https://goreportcard.com/badge/go.dtapp.net/godecimal)](https://goreportcard.com/report/go.dtapp.net/godecimal)
|
||||
[![deps.dev](https://img.shields.io/badge/deps-go-red.svg)](https://deps.dev/go/go.dtapp.net%2Fgodecimal)
|
||||
|
||||
#### 安装
|
||||
|
||||
```shell
|
||||
go get -v -u go.dtapp.net/godecimal@v1.0.11
|
||||
```
|
||||
|
||||
#### 使用
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"go.dtapp.net/godecimal"
|
||||
"log"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func main() {
|
||||
log.Println("加:", godecimal.Float64Add(10, 3), reflect.TypeOf(godecimal.Float64Add(10, 3)))
|
||||
log.Println("减", godecimal.Float64Sub(10, 3), reflect.TypeOf(godecimal.Float64Sub(10, 3)))
|
||||
log.Println("乘:", godecimal.Float64Mul(10, 3), reflect.TypeOf(godecimal.Float64Mul(10, 3)))
|
||||
log.Println("除:", godecimal.Float64Quo(10, 3), reflect.TypeOf(godecimal.Float64Quo(10, 3)))
|
||||
}
|
||||
```
|
@ -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,38 @@
|
||||
package godecimal
|
||||
|
||||
import "fmt"
|
||||
|
||||
// NewInterface 创建
|
||||
func NewInterface(value interface{}) Decimal {
|
||||
d := New()
|
||||
d.floatValue.SetString(fmt.Sprint(value))
|
||||
return d
|
||||
}
|
||||
|
||||
// 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,5 +1,3 @@
|
||||
module go.dtapp.net/godecimal
|
||||
|
||||
go 1.18
|
||||
|
||||
require go.dtapp.net/gostring v1.0.3
|
||||
go 1.21
|
||||
|
@ -1,2 +0,0 @@
|
||||
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=
|
@ -1,115 +0,0 @@
|
||||
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)
|
||||
}
|
@ -1,16 +0,0 @@
|
||||
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))
|
||||
}
|
@ -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)
|
||||
}
|
@ -1,3 +1,3 @@
|
||||
package godecimal
|
||||
|
||||
const Version = "1.0.0"
|
||||
const Version = "1.0.11"
|
||||
|
@ -1,7 +0,0 @@
|
||||
package godecimal
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestVersion(t *testing.T) {
|
||||
t.Log(Version)
|
||||
}
|
Loading…
Reference in new issue