Rails格式化BigDecimal

时间:2014-01-03 09:47:32

标签: ruby-on-rails formatting bigdecimal

我的问题很简单(我认为):

在Rails中,我有自定义日期时间格式函数,名为 l ,BigDecimal有一个等效函数吗?

我想要的例子:

index.html.erb

<%= l bill.date, format: :od %>
<%= ? bill.total_amount, format: :o2d %>
<%= ? bill.tax_amount, format: :o2d %>

我想用货币符号代表BigDecimal作为货币,但现在我使用:

<%= number_to_currency bill.total_amount, unit: '&euro;', separator: ',', delimiter: '.', format: '%n %u' %>

看起来很脏?

提前致谢。

2 个答案:

答案 0 :(得分:1)

这个红宝石宝石会帮助你。

https://github.com/RubyMoney/money

可能你必须实现自己的视图助手,你应该在其中实现如下。

money = Money.new(1000, "USD")
money.cents => 1000
money.symbol => $    

def money_helper(money)
  "#{money.symbol}#{money.cents}" 
end

<%= money_helper 1000 %> => $1000

更多配置:

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


The pre-defined set of attributes includes:

:priority a numerical value you can use to sort/group the currency list
:iso_code the international 3-letter code as defined by the ISO 4217 standard
:iso_numeric the international 3-digit code as defined by the ISO 4217 standard
:name the currency name
:symbol the currency symbol (UTF-8 encoded)
:subunit the name of the fractional monetary unit
:subunit_to_unit the proportion between the unit and the subunit
:separator character between the whole and fraction amounts
:delimiter character between each thousands place

答案 1 :(得分:1)

您可以为number_to_currency bill.total_amount, unit: '&euro;', separator: ',', delimiter: '.', format: '%n %u'

中的操作定义自己的帮助程序
def money value
  number_to_currency value, unit: '&euro;', separator: ',', delimiter: '.', format: '%n %u'
end

并像<%= money(bill.total_amount) %>

一样使用它