具有十进制精度的格式数字字段? [英] Formtastic number field with decimal precision?

查看:54
本文介绍了具有十进制精度的格式数字字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确实遗漏了一些非常明显的东西......我有一个精度为 2 的十进制字段,但 Formtastic 只显示一个小数,除非实际值有 2 个位置.我错过了什么?

Surely I'm missing something pretty obvious... I have a field that is decimal with precision 2, but Formtastic is displaying it with only a single decimal, unless the actual value has 2 places. What am I missing?

型号:

create_table "items", :force => true do |t|
    t.string   "item_number"
    t.integer  "buyer_id"
    t.integer  "seller_id"
    t.string   "description"
    t.decimal  "sales_price", :precision => 10, :scale => 2, :default => 0.0
    t.datetime "created_at"
    t.datetime "updated_at"
end

查看

%td= bought.input :sales_price, input_html: { class: 'span2'}, label: false

注意下面的答案,其他人稍后可能不清楚:

Noting answer from below, which might not be clear to others finding this later:

%td= bought.input :sales_price, input_html: { class: 'span2', value: number_with_precision(bought.object.sales_price, precision: 2)}, label: false

推荐答案

试试这个:

%td= bought.input :sales_price, input_html: { class: 'span2', value: number_with_precision(bought.sales_price, precision: 2) }, label: false

Sales_price 以两位小数存储在您的数据库中,但您必须在显示值时告诉 rails 以这种方式对其进行格式化.

Sales_price is being stored in your database with two decimal places, but you have to tell rails to format it that way when displaying the value.

这篇关于具有十进制精度的格式数字字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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