使用money-rails minidollars单位而不是美分

时间:2015-05-22 09:50:10

标签: ruby-on-rails ruby ruby-on-rails-4 money-rails

默认情况下,以美分为单位省钱,并创建像price_cents一样的迁移:整数。我需要以小数(1/1000货币)保存和计算价值。

我自己没有注册货币。 我使用gem google_currency获取当前货币汇率。

如何为所有货币值设置精度1/1000?

  * rails (4.2.1)
  * money (6.5.1)
  * money-rails (1.4.1)
  * google_currency (3.2.0)

1 个答案:

答案 0 :(得分:0)

根据here,您可以注册自己的子单元:

curr = {
  :priority        => 1,
  :iso_code        => "USD",
  :iso_numeric     => "840",
  :name            => "United States Dollar",
  :symbol          => "$",
  :subunit         => "Minidollar",
  :subunit_to_unit => 1000,
  :separator       => ".",
  :delimiter       => ","
}

Money::Currency.register(curr)

Google货币只会延长Money::Bank::VariableExchange,所以现在你要创建一个$ 1商品并以欧元获得价值:

money = Money.new(10_00, "USD") # this is in "minidollars"
money.exchange_to(:EUR)