无法在 Rails 3 的文本字段中显示小数

发布于 2024-11-27 18:53:10 字数 1539 浏览 1 评论 0原文

我有一个简短的问题......对 Rails 相当陌生,我想知道你是否可以帮助我解决我在代码中遇到的问题。我有一组输入框(这是一个询问收入和支出的在线应用程序),我想在其中设置货币格式。我一直在应用程序内的其他独立输入框(text_fields)中使用以下代码行,但由于某种原因,当我尝试将其添加到

我一直使用的损益表字段代码

= f.text_field :user_nummber, :value=>number_to_currency(f.object.user_number)

中 时,它不会工作:我一直遇到问题的代码:

 = f.fields_for :income_statements do |ff|
        .section_total.income_statement{ :id => "income_statement_#{ff.object.year}" }
          = ff.hidden_field :id
          = ff.hidden_field :year
          %h3.financials_year= ff.object.year

          .financials_data
            .section
              %h2.green_text Revenue
              %div{:class => "label_rightalign_investor field"}
                = ff.label :net_sales
                = ff.text_field :net_sales, :value=>number_to_currency(ff.object.net_sales), :class => 'add'
              %div{:class => "label_rightalign_investor field"}
                = ff.label :interest_income
                = ff.text_field :interest_income, :value=>number_to_currency(ff.object.interest_income), :class => 'add'
              %div{:class => "label_rightalign_investor field", :style => 'font-weight: bold' }
                = label_tag 'Total'
                = text_field_tag 'total_revenue', '', :class => 'calculate add_total', :readonly => true

请注意,最终的 Total_revenue 字段使用 class:add (净销售额和利息收入) 计算值

对于我做错的事情以及为什么我的 text_fields (输入框) 这样做有任何帮助不显示值以下格式:$x,xxx.00 将不胜感激。

我衷心感谢您的帮助和时间!

I have a brief question... pretty new to rails, and I was wondering if you could help me with an issue that I'm having in code. I have a set of input boxes (it's an online app that asks for revenues and expenses) where I would like to format the currency. I've been using the following line of code for other independent input boxes (text_fields) within the app, but for some reason, it wont work when i try to add it to the income statement fields

code I have been using:

= f.text_field :user_nummber, :value=>number_to_currency(f.object.user_number)

the area of code i've been having issues with:

 = f.fields_for :income_statements do |ff|
        .section_total.income_statement{ :id => "income_statement_#{ff.object.year}" }
          = ff.hidden_field :id
          = ff.hidden_field :year
          %h3.financials_year= ff.object.year

          .financials_data
            .section
              %h2.green_text Revenue
              %div{:class => "label_rightalign_investor field"}
                = ff.label :net_sales
                = ff.text_field :net_sales, :value=>number_to_currency(ff.object.net_sales), :class => 'add'
              %div{:class => "label_rightalign_investor field"}
                = ff.label :interest_income
                = ff.text_field :interest_income, :value=>number_to_currency(ff.object.interest_income), :class => 'add'
              %div{:class => "label_rightalign_investor field", :style => 'font-weight: bold' }
                = label_tag 'Total'
                = text_field_tag 'total_revenue', '', :class => 'calculate add_total', :readonly => true

Please note that the final total_revenue field calculates the values with class:add (net sales & interest income)

Any help in what i'm doing wrong, and why my text_fields (input box) does not display values in the following format: $x,xxx.00 would be appreciated.

I sincerely thank you for your help and time!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神爱温柔 2024-12-04 18:53:10

尝试添加精度参数,如下所示:

<%= f.text_field :user_number, :value => number_to_currency(f.object.user_number, :precision => 3) %>

查看此处了解更多详细信息:
http://api.rubyonrails.org/classes/ActionView /Helpers/NumberHelper.html#method-i-number_to_currency

希望它能对您有所帮助!

Try add the precision parameter like this:

<%= f.text_field :user_number, :value => number_to_currency(f.object.user_number, :precision => 3) %>

Check here for more details:
http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html#method-i-number_to_currency

Hope it helps you somehow!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文