Rails 3.如何在编辑表单中显示两位小数位? [英] Rails 3. How to display two decimal places in edit form?

查看:136
本文介绍了Rails 3.如何在编辑表单中显示两位小数位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个编辑表单。



但是,当我存储诸如1.5的东西时,我想将其显示为1.50。



我怎么可以用表单助手来做这件事? <%= f.text_field:cost,:class => 'cost'%>

解决方案

您应该使用 number_with_precision helper。 查看文档



示例:

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

在你的表格助手中:

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

顺便说一句,如果您真的想显示一些价格,请使用 number_to_currency ,doc的相同页面(在表单上下文中,我会保留 number_with_precision ,你不想搞乱钱符号)





I have this edit form.

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

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

解决方案

You should use number_with_precision helper. See doc.

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)


这篇关于Rails 3.如何在编辑表单中显示两位小数位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆