Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using number_with_precision on controller in rails3

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

like image 903
mlzboy Avatar asked Dec 13 '10 06:12

mlzboy


2 Answers

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"
like image 146
Mark Oliver Avatar answered Nov 22 '22 07:11

Mark Oliver


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
like image 27
jenjenut233 Avatar answered Nov 22 '22 07:11

jenjenut233