diff --git a/.gitignore b/.gitignore index 442794a..a037fd2 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ .vscode *.log gomod.sh -/vendor/ \ No newline at end of file +/vendor/ +*_test.go \ No newline at end of file diff --git a/big_output.go b/big_output.go index 53818e7..85a8e52 100644 --- a/big_output.go +++ b/big_output.go @@ -1,7 +1,9 @@ package godecimal import ( + "fmt" "math/big" + "strings" ) // String 输出 string @@ -23,9 +25,34 @@ func (d Decimal) MoneyFloat64() float64 { return f } -// Float64Point 输出 float64带小数点 +// 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 { + pointLength := d.pointLength(f) + if pointLength > 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]) +} diff --git a/big_test.go b/big_test.go deleted file mode 100644 index f4bb032..0000000 --- a/big_test.go +++ /dev/null @@ -1,66 +0,0 @@ -package godecimal - -import ( - "math/big" - "reflect" - "testing" -) - -func TestNew(t *testing.T) { - - priceString := NewString("100") - t.Log("从字符串创建:", priceString) - - priceFloat := NewFloat(0.20) - t.Log("从浮点数创建:", priceFloat) - - priceInt := NewInt(300) - t.Log("从整数创建:", priceInt) - - priceUint := NewUint(400) - t.Log("从无符合整数创建:", priceUint) - - t.Log("加:", priceString.Add(priceFloat), reflect.TypeOf(priceString)) - t.Log("减", priceString.Sub(priceFloat), reflect.TypeOf(priceString)) - t.Log("乘:", priceString.Mul(priceFloat), reflect.TypeOf(priceString)) - t.Log("除:", priceString.Quo(priceFloat), reflect.TypeOf(priceString)) -} - -// https://www.itranslater.com/qa/details/2582643503239005184 -// https://stackoverflow.com/questions/18390266/how-can-we-truncate-float64-type-to-a-particular-precision -func TestOperation(t *testing.T) { - - a1 := NewFloat(0.1890) - t.Log("a1:", a1) - - a2 := NewFloat(0.50) - t.Log("a2:", a2) - - t.Log("a1+a2:", a1.Add(a2), a1.Add(a2).String(), a1.Add(a2).Float64(), reflect.TypeOf(a1.Add(a2).Float64()), a1.Add(a2).MoneyFloat64(), reflect.TypeOf(a1.Add(a2).MoneyFloat64())) - t.Log("a1-a2:", a1.Sub(a2), a1.Sub(a2).String(), a1.Sub(a2).Float64(), reflect.TypeOf(a1.Sub(a2).Float64()), a1.Sub(a2).MoneyFloat64(), reflect.TypeOf(a1.Sub(a2).MoneyFloat64())) - t.Log("a1*a2:", a1.Mul(a2), a1.Mul(a2).String(), a1.Mul(a2).Float64(), reflect.TypeOf(a1.Mul(a2).Float64()), a1.Mul(a2).MoneyFloat64(), reflect.TypeOf(a1.Mul(a2).MoneyFloat64())) - t.Log("a1/a2:", a1.Quo(a2), a1.Quo(a2).String(), a1.Quo(a2).Float64(), reflect.TypeOf(a1.Quo(a2).Float64()), a1.Quo(a2).MoneyFloat64(), reflect.TypeOf(a1.Quo(a2).MoneyFloat64())) - - a3 := NewFloat(.035) - t.Log("a3:", a3) - - a4 := NewFloat(.08875) - t.Log("a4:", a4) - - t.Log("a3+a4:", a3.Add(a4), a3.Add(a4).String(), a3.Add(a4).Float64(), reflect.TypeOf(a3.Add(a4).Float64()), a3.Add(a4).MoneyFloat64(), reflect.TypeOf(a3.Add(a4).MoneyFloat64())) - t.Log("a3-a4:", a3.Sub(a4), a3.Sub(a4).String(), a3.Sub(a4).Float64(), reflect.TypeOf(a3.Sub(a4).Float64()), a3.Sub(a4).MoneyFloat64(), reflect.TypeOf(a3.Sub(a4).MoneyFloat64())) - t.Log("a3*a4:", a3.Mul(a4), a3.Mul(a4).String(), a3.Mul(a4).Float64(), reflect.TypeOf(a3.Mul(a4).Float64()), a3.Mul(a4).MoneyFloat64(), reflect.TypeOf(a3.Mul(a4).MoneyFloat64())) - t.Log("a3/a4:", a3.Quo(a4), a3.Quo(a4).String(), a3.Quo(a4).Float64(), reflect.TypeOf(a3.Quo(a4).Float64()), a3.Quo(a4).MoneyFloat64(), reflect.TypeOf(a3.Quo(a4).MoneyFloat64())) -} - -func Test3(t *testing.T) { - - var x1, y1 float64 = 10, 3 - z1 := x1 / y1 - t.Log(x1, y1, z1) - - x2, y2 := big.NewFloat(10), big.NewFloat(3) - z2 := new(big.Float).Quo(x2, y2) - t.Log(x2, y2, z2) - -} diff --git a/calculate_test.go b/calculate_test.go deleted file mode 100644 index 43f56f3..0000000 --- a/calculate_test.go +++ /dev/null @@ -1,13 +0,0 @@ -package godecimal - -import ( - "reflect" - "testing" -) - -func TestCalculation(t *testing.T) { - t.Log("加:", Float64Add(10, 3), reflect.TypeOf(Float64Add(10, 3))) - t.Log("减", Float64Sub(10, 3), reflect.TypeOf(Float64Sub(10, 3))) - t.Log("乘:", Float64Mul(10, 3), reflect.TypeOf(Float64Mul(10, 3))) - t.Log("除:", Float64Quo(10, 3), reflect.TypeOf(Float64Quo(10, 3))) -} diff --git a/math_test.go b/math_test.go deleted file mode 100644 index 0197d83..0000000 --- a/math_test.go +++ /dev/null @@ -1,26 +0,0 @@ -package godecimal - -import "testing" - -func TestMath(t *testing.T) { - t.Log(Abs(111)) - t.Log(Abs(-111)) - - t.Log(Floor(3.4)) - t.Log(Floor(3.5)) - - t.Log(Ceil(3.4)) - t.Log(Ceil(3.5)) - - t.Log(Round(3.4)) - t.Log(Round(3.5)) - - t.Log(RoundPoint(3.4)) - t.Log(RoundPoint(3.5)) - t.Log(RoundPoint(3.14)) - t.Log(RoundPoint(3.14159)) - - t.Log(Max(4, 3)) - - t.Log(Min(4, 3)) -} diff --git a/version.go b/version.go index e9d0af9..f083e35 100644 --- a/version.go +++ b/version.go @@ -1,3 +1,3 @@ package godecimal -const Version = "1.0.6" +const Version = "1.0.7" diff --git a/version_test.go b/version_test.go deleted file mode 100644 index 750d967..0000000 --- a/version_test.go +++ /dev/null @@ -1,7 +0,0 @@ -package godecimal - -import "testing" - -func TestVersion(t *testing.T) { - t.Log(Version) -}