Using number_with_precision on controller in rails3

旧巷老猫 提交于 2019-12-23 07:03:17

问题


I want to use this helper method on controller. Any way to achieve this?


回答1:


Probably not a great idea, but if you must, include the helper like so:

class WhateverController
  include ActionView::Helpers::NumberHelper

  def show
    render :text => number_with_precision(2342.234, :precision => 2)
  end

end



回答2:


A much better way to control number precision is rails controllers is to use sprintf

"%.2f" % 2342.234 => "2342.23"
"%.6f" % 2342.234 => "2342.234000"


来源:https://stackoverflow.com/questions/4426315/using-number-with-precision-on-controller-in-rails3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!