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

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

问题描述

我有这个编辑表单.

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

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

我怎么能用表单助手做到这一点?<%= f.text_field :cost, :class =>'成本' %>

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

推荐答案

你应该使用 number_with_precision 助手.参见文档.

You should use number_with_precision helper. See doc.

示例:

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,文档的同一页面(在表单上下文中,我会保留 number_with_precision,你不不想弄乱货币符号)

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天全站免登陆