Always show 2 decimal places

Hi,

I have a float that often looks like somthing like 2003.2 in my
controller (1 decimal place)

Is there a way to always show it to 2 decimal places eg 2003.20 ?

Thanks
Scott

Ruby uses"printf" format conventions.

You can use:

printf (“%.2f”,2003.2) ==> 2003.20

_Hari

View this message in context:
http://www.nabble.com/always-show-2-decimal-places-tf1872577.html#a5118477
Sent from the RubyOnRails Users forum at Nabble.com.

Nara H. wrote:

Ruby uses"printf" format conventions.

You can use:

printf ("%.2f",2003.2) ==> 2003.20

I need to be able to store this in a nother varibale in a controller, is
this possible?

Just FYI: If you see only one decimal place inside of a form field, you
might want to look at the precision (number of decimal places) in your
database. For example, MySQL defaults to 1 for floats. As soon as you
change
the precision to 2, you’ll see 2 places. This is, of course, only when
using <%=text_field %> and similar.

sprintf("%.2f", number)

just an idea:

class Float
def to_s(digits)
sprintf("%.#{digits}f", self)
end
end

12.0.to_s(2)

12.00

ss schrieb: