微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ruby-on-rails – 带小数精度的Formtastic数字字段?

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

模型:

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

注意到下面的回答,其他人在以后发现这个问题可能并不清楚:

%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,value: number_with_precision(bought.sales_price,precision: 2) },label: false

Sales_price存储在数据库中,带有两个小数位,但您必须告诉rails在显示值时将其格式化.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐