代码之家  ›  专栏  ›  技术社区  ›  VP.

mongoid中的货币表示

  •  5
  • VP.  · 技术社区  · 15 年前

    我该如何处理 Money

    4 回复  |  直到 15 年前
        1
  •  3
  •   dm.    15 年前

    MongoDB以不同的方式存储数字 BSON 数据类型(int、long int、double)。我建议您将货币存储为美分(如果是美国货币),并使用long int数据类型。

        2
  •  8
  •   Brian Armstrong    14 年前

    Integer似乎是一种以美分为单位存储值的方法,可能是使用Money gem对其进行一点抽象: https://github.com/RubyMoney/money

    Mongo在大多数现代机器上使用64位存储int,我认为这样即使以美分计算也不会有太大的风险。看起来你可以存储9223372036854775808到9223372036854775807美分,所以去掉两个小数位来得到你的最小/最大美元值。

    http://en.wikipedia.org/wiki/Integer_(computer_science)

        3
  •  2
  •   Michael Koper    13 年前

    如果你喜欢货币宝石,你可以储存它作为一个货币类型。

    举个例子: https://gist.github.com/michaelkoper/5007636

    它将钱存储为数组[美分,货币]

    class Product
       include Mongoid::Document
    
      field :price,    type: Money
    end
    
    product = Product.new(:price => Money.new(1000, 'EUR'))
    product.price.format
    # => "€10.00"
    
        4
  •  0
  •   P Pramod Nair    13 年前

    我建议您尝试使用money rails作为替代方案。 https://github.com/RubyMoney/money-rails 它是相当好的维护和工作与mongoid太!