form.text_field display of sql decimal

hey all,

I've got this column in a mysql table:
wholesale_price decimal(6,2)

this is the actual value that is currently stored in the db:

but then the rendered html source looks like this:
<input id="film_wholesale_price" name="film[wholesale_price]" size="5"
type="text" value="19.0" />

when I want it to look like this:
<input id="film_wholesale_price" name="film[wholesale_price]" size="5"
type="text" value="19.00" /> <-- note the extra "0"

imho its better to use textfield in the db, and store price data in the
'Money' type (off course, you must serialize this field in your model).

don't forget to install money.gem and `require` it.

That’s a good approach.

When I first started using Rails, I found this problem and it bugged me so I tried to write a plugin to override this behavior. I got it working but it was way more trouble than it was worth.

My approach was to add two new methods to my model.

#f retreives the wholesale price and formats it as a decimal
def format_wholesale_price
sprintf("%01.2f", self.wholesale_price
end

Sets the value (needed to make the form helper work

def format_wholesale_price=(value)
self.wholesale_price = value
end

Then just use those in your helpers.

<%= form.text_field :format_wholesale_price %>

That should work.