|
|
@ -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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|