我正在寫一個應用程序,使用金錢並且想要非常準確的數字。我也使用mgo來存儲一些應用程序後的結果。我想知道是否有一種方法可以讓我在結構中使用math.Rat
或godec,並將它存儲爲mgo中的數字?使用mgo精確計算小數點
這是代碼我希望運行的那種:
package main
import(
"fmt"
"math/big"
"labix.org/v2/mgo"
)
var mgoSession *mgo.Session
type 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)
}
而你的代碼不會運行,因爲...? – nemo
因爲mongo db不知道如何存儲big.Rat – Gary
你可以只存儲大數字的「STRING」表示 – fabrizioM