# Form.text\_field display of sql decimal

**URL:** <https://www.ruby-forum.com/t/form-text-field-display-of-sql-decimal/92395>\
**Category:** Rails\
**Created:** [April 19, 2007, 2:07am UTC](https://www.ruby-forum.com/t/form-text-field-display-of-sql-decimal/92395 "2007-04-19T02:07:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 19, 2007, 2:07am UTC](https://www.ruby-forum.com/t/form-text-field-display-of-sql-decimal/92395/1 "2007-04-19T02:07:19Z")

</div>

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:  
±----------------+  
| wholesale\_price |  
±----------------+  
| 19.00 |  
±----------------+

the the view code to edit that:  
\<%= form.text\_field :wholesale\_price %\>

but then the rendered html source looks like this:

when I want it to look like this:  
 \<-- note the extra “0”

I mucked around with creating a helper method to fix that up but seem  
to be missing something simple/obvious.

Thanks in advance for any help!  
Tim

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 19, 2007, 2:57am UTC](https://www.ruby-forum.com/t/form-text-field-display-of-sql-decimal/92395/2 "2007-04-19T02:57:21Z")

</div>

> but then the rendered html source looks like this:
> 
> when I want it to look like this:  
> ← 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.

–  
wbr, sh0ckfile.  
FileForum moders team  
[http://www.fforum.ru](http://www.fforum.ru)

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 19, 2007, 2:52pm UTC](https://www.ruby-forum.com/t/form-text-field-display-of-sql-decimal/92395/3 "2007-04-19T14:52:37Z")

</div>

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.
