1

我在我的 Rails 4 应用程序中使用 money-rails。

我显示我的货币化属性没有美分。我希望用户只输入整美元/英镑的金额(所以不是 100 是 1 美元,我希望它是 100 美元)。

在我的表格中,我问用户:

        <%= par.input :participation_cost_pennies, label: false, placeholder: 'Whole numbers only', :input_html => {:style => 'width: 250px; margin-top: 20px', class: 'response-project'} %>

在我的节目中,我有:

   <%= money_without_cents_and_with_symbol @project.scope.participant.participation_cost  %>

我想知道如何将 .00 添加到用户在表单中输入的任何内容中,或者最好将输入的值设为美元金额而不是美分金额。有谁知道这是怎么做到的吗?

我已经删除了我的 money.rb 初始化程序中的美分:

config.default_format = {
     :no_cents_if_whole => nil,
4

1 回答 1

0

使用金钱时,我喜欢使用DECIMAL而不是INTEGERS在我的数据库中使用。

在您的迁移中,请执行以下操作:

add_column :items, :price, :decimal, :precision => 8, :scale => 2
# precision is the total amount of digits
# scale is the number of digits right of the decimal point

在 Rails 中,:decimal类型返回为BigDecimal,这对于价格计算非常有用。

如果您坚持使用整数,您将不得不在BigDecimal任何地方手动转换为 s 和从 s 转换。

要打印价格,请使用:

number_to_currency(price, :unit => "$")
#=> $1,234.01

看看 http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html#method-i-number_to_currency

https://stackoverflow.com/a/1019972/1380867

于 2015-06-13T09:56:58.377 回答