- add IsInteger

master
李光春 2 years ago
parent 56bdfc6a73
commit d876f75d9f

@ -2,6 +2,7 @@ package godecimal
import (
"fmt"
"math"
"math/big"
"strings"
)
@ -36,8 +37,8 @@ func (d Decimal) Float64Point(p int) float64 {
func (d Decimal) Float64PointAdaptive(maxP int) float64 {
f, _ := d.floatValue.Float64()
if maxP > 0 {
pointLength := d.pointLength(f)
if pointLength > maxP {
pL := d.pointLength(f)
if pL > maxP {
rat, _ := new(big.Rat).SetString(d.floatValue.Text('f', maxP))
f2, _ := rat.Float64()
return f2
@ -56,3 +57,12 @@ func (Decimal) pointLength(a any) int {
}
return len(tmp[1])
}
// IsInteger 是否为整数
func (d Decimal) IsInteger(d2 float64) bool {
f3 := NewFloat(d.Float64()).QuoFloat(NewFloat(d2).Float64()).Float64()
if f3 == math.Trunc(f3) {
return true
}
return false
}

@ -1,3 +1,5 @@
module go.dtapp.net/godecimal
go 1.19
require github.com/shopspring/decimal v1.3.1

@ -0,0 +1,2 @@
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=

@ -1,3 +1,3 @@
package godecimal
const Version = "1.0.7"
const Version = "1.0.8"

Loading…
Cancel
Save