英文:
Go precise decimals with mgo
问题
我正在编写一个应用程序,其中涉及到货币,并且需要非常精确的数字。我还使用mgo将结果存储在应用程序之后。我想知道是否有办法在结构体中使用math.Rat
或godec,并将其作为数字存储在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)
}
以上是您要运行的代码。
英文:
I am writing an application where I use money and want very accurate numbers. I am also using mgo to store the results after some application. I was wondering if there was a way for me to use math.Rat
or godec in a struct and have it store as a number in mgo?
This is the kind of code i was hoping to run:
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)
}
答案1
得分: 3
big.Rat
基本上是一对未导出的big.Int
值,分别描述有理数的分子和分母。
你可以通过(*big.Rat).Denom
和(*big.Rat).Num
轻松获取这两个数字。
然后将它们存储在自己的结构中,使用导出的(大写)字段:
type CurrencyValue struct {
Denom int64
Num int64
}
使用mgo
将其存储,并通过big.NewRat
将其转换回*big.Rat
。
编辑:
评论中的Nick Craig-Wood正确指出,big.Rat
实际上由2个big.Int
值组成,而不是我之前写的int
值(很容易忽略大写的i)。在BSON中很难表示一个big.Int
,但是int64
应该可以满足大多数用例。
英文:
big.Rat
is basically a pair of unexported <strike>int
</strike> big.Int
values describing the numerator and denominator of a rational number, respectively.
You can easily get both numbers through (*big.Rat).Denom
and (*big.Rat).Num
.
Then store them in a structure of your own, with exported (upper case) fields:
type CurrencyValue struct {
Denom int64
Num int64
}
Store this with mgo
and convert it back to a *big.Rat
in your application through big.NewRat
Edit:
Nick Craig-Wood in the comments correctly noted that big.Rat
actually consists of 2 big.Int
values, not int
values as I had written (easy to miss the upper case i). It's hard to represent a big.Int
in BSON but, int64
should cover most use-cases.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论