我正在編寫一個應(yīng)用程序,我在其中使用金錢并想要非常準(zhǔn)確的數(shù)字。我也在使用 mgo 來存儲一些應(yīng)用程序后的結(jié)果。我想知道是否有辦法math.Rat在結(jié)構(gòu)中使用或 godec 并將其作為數(shù)字存儲在 mgo 中?這是我希望運(yùn)行的代碼類型:package mainimport( "fmt" "math/big" "labix.org/v2/mgo")var mgoSession *mgo.Sessiontype Test struct{ Budget big.Rat}func MongoLog(table string, pointer interface{}) { err := mgoSession.DB("db_log").C(table).Insert(pointer) if err != nil { panic(err) }}func main(){ var err error mgoSession, err = mgo.Dial("localhost:27017") defer mgoSession.Close() if err != nil { panic(err) } cmp := big.NewRat(1, 100000) var test = Test{Budget : *big.NewRat(5, 10)} MongoLog("test", &test) for i := 0; i < 20; i++{ fmt.Printf("Printf: %s\n", test.Budget.FloatString(10)) fmt.Println("Println:", test.Budget, "\n") test.Budget.Sub(&test.Budget, cmp)// test.Budget = test.Budget - cpm } MongoLog("test", &test)}
使用 mgo 精確計算小數(shù)
慕標(biāo)琳琳
2021-07-16 18:11:34