Rails 3。如何在编辑表单中显示两位小数?

时间:2021-07-18 16:36:00

I have this edit form.

我有这个编辑表单。

But when I store something such as 1.5, I would like to display it as 1.50.

但是当我存储1。5的时候,我想把它显示为1。50。

How could I do that with the form helper? <%= f.text_field :cost, :class => 'cost' %>

我怎么能用帮助表来做呢?< % = f。text_field:cost,:class => 'cost' %>

4 个解决方案

#1


151  

You should use number_with_precision helper. See doc.

您应该使用number_with_precision助手。看医生。

Example:

例子:

number_with_precision(1.5, :precision => 2)
=> 1.50 

Within you form helper:

在你表单辅助:

<%= f.text_field :cost, :class => 'cost', :value => (number_with_precision(f.object.cost, :precision => 2) || 0) %>

BTW, if you really want to display some price, use number_to_currency, same page for doc (In a form context, I'd keep number_with_precision, you don't want to mess up with money symbols)

顺便说一句,如果你真的想要显示一些价格,可以使用number_to_currency和doc的相同页面(在表单上下文中,我将保留number_with_precision,您不会希望弄乱货币符号)


#2


44  

Alternatively, you can use the format string "%.2f" % 1.5. http://ruby-doc.org/docs/ProgrammingRuby/html/ref_m_kernel.html#Kernel.sprintf

或者,您可以使用格式字符串“%”。2 f“% 1.5。http://ruby-doc.org/docs/ProgrammingRuby/html/ref_m_kernel.html Kernel.sprintf

#3


9  

For this I use the number_to_currency formater. Since I am in the US the defaults work fine for me.

为此,我使用number_to_currency格式器。因为我在美国,默认设置对我来说很有效。

<% price = 45.9999 %>
<price><%= number_to_currency(price)%></price>
=> <price>$45.99</price>

You can also pass in options if the defaults don't work for you. Documentation on available options at api.rubyonrails.org

如果默认值对您不起作用,您也可以传入选项。关于api.rubyonrails.org的可用选项的文档。

#4


7  

Rails has a number_to_currency helper method which might fit you specific use case better.

Rails有一个number_to_currency助手方法,它可能更适合您的特定用例。

#1


151  

You should use number_with_precision helper. See doc.

您应该使用number_with_precision助手。看医生。

Example:

例子:

number_with_precision(1.5, :precision => 2)
=> 1.50 

Within you form helper:

在你表单辅助:

<%= f.text_field :cost, :class => 'cost', :value => (number_with_precision(f.object.cost, :precision => 2) || 0) %>

BTW, if you really want to display some price, use number_to_currency, same page for doc (In a form context, I'd keep number_with_precision, you don't want to mess up with money symbols)

顺便说一句,如果你真的想要显示一些价格,可以使用number_to_currency和doc的相同页面(在表单上下文中,我将保留number_with_precision,您不会希望弄乱货币符号)


#2


44  

Alternatively, you can use the format string "%.2f" % 1.5. http://ruby-doc.org/docs/ProgrammingRuby/html/ref_m_kernel.html#Kernel.sprintf

或者,您可以使用格式字符串“%”。2 f“% 1.5。http://ruby-doc.org/docs/ProgrammingRuby/html/ref_m_kernel.html Kernel.sprintf

#3


9  

For this I use the number_to_currency formater. Since I am in the US the defaults work fine for me.

为此,我使用number_to_currency格式器。因为我在美国,默认设置对我来说很有效。

<% price = 45.9999 %>
<price><%= number_to_currency(price)%></price>
=> <price>$45.99</price>

You can also pass in options if the defaults don't work for you. Documentation on available options at api.rubyonrails.org

如果默认值对您不起作用,您也可以传入选项。关于api.rubyonrails.org的可用选项的文档。

#4


7  

Rails has a number_to_currency helper method which might fit you specific use case better.

Rails有一个number_to_currency助手方法,它可能更适合您的特定用例。