我想humanized_money_with_symbol
方法返回类似的东西USD$ 100
,不仅如此$ 100
。我也想只在货币符号是时才这样做$
,我们想让用户知道什么时候$
是美元,什么时候是澳元。
问问题
3648 次
3 回答
1
您可以覆盖 USD 配置以initializers/money.rb
将“USD”显示为符号的一部分:
MoneyRails.configure do |config|
config.register_currency = {
"priority": 2,
"iso_code": "USD",
"name": "United States Dollar",
"symbol": "USD $",
"subunit": "Cent",
"subunit_to_unit": 100,
"symbol_first": true,
"decimal_mark": ".",
"thousands_separator": ",",
}
end
重新启动服务器,您应该会看到“USD $100”。我不使用多种货币,但这应该让您的其他货币正常显示。
于 2017-07-12T11:18:54.740 回答
1
从未使用过 MoneyRails,但它看起来 humanized_money_with_symbol
只是将调用humanized_money
合并symbol: true
到您传递的参数中。
然后该助手依次调用format
传入的货币对象,传入您指定的选项。在Money gem 中,您可以传入 a:symbol
以呈现货币,例如
m = Money.new('123', :gbp) # => #<Money fractional:123 currency:GBP>
m.format( symbol: m.currency.to_s + ' ') # => "GBP 1.23"
所以,如果你打电话
humanized_money(Money.new('123', :usd), symbol: 'USD $')
# => "USD $1.23"
然后,您可以在您的应用程序中设置一个辅助方法,以避免始终必须传递该符号,例如:
def render_custom_currency(value, options = {})
value.currency.iso_code == "USD" ? humanized_money(value, options.merge(symbol: 'USD $')) : humanized_money(value, options.merge(symbol: true))
end
这应该让你得到你想要做的事情。
于 2017-07-12T10:44:02.913 回答
1
最后我使用了 MoneyRails gem option 中的内置选项disambiguate: true
。
要使用它,您可以调用如下方法:
humanized_money_with_symbol(value, disambiguate: true)
这里有一些它是如何工作的例子
于 2017-07-13T19:14:07.777 回答