# Formatting values of numbers using text\_field

**URL:** <https://www.ruby-forum.com/t/formatting-values-of-numbers-using-text-field/72938>\
**Category:** Rails\
**Created:** [February 24, 2006, 6:09pm UTC](https://www.ruby-forum.com/t/formatting-values-of-numbers-using-text-field/72938 "2006-02-24T18:09:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![HoganS\_Brian\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/9de0a6/32.png) [@HoganS\_Brian\_P](https://www.ruby-forum.com/u/HoganS_Brian_P)\
**Post date:** [February 24, 2006, 6:09pm UTC](https://www.ruby-forum.com/t/formatting-values-of-numbers-using-text-field/72938/1 "2006-02-24T18:09:53Z")

</div>

Anyone have a quick solution on how to format a number in a text\_field  
tag?

\<% text\_field “price”, “amount” %\> returns 100.0 and it would be nice  
if it showed 100.00

Of course, number\_with\_precision works nicely, but how do you make that  
work with text\_field?

I thought maybe overriding the getter in the model. However, this means  
I would have to bring the helper code into the model which I’m not sure  
I like.

Brian H.  
Web D.  
Learning & Technology Services  
Schofield 3-B  
University of Wisconsin-Eau Claire  
715 836 3585  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![HoganS\_Brian\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/9de0a6/32.png) [@HoganS\_Brian\_P](https://www.ruby-forum.com/u/HoganS_Brian_P)\
**Post date:** [February 26, 2006, 12:35pm UTC](https://www.ruby-forum.com/t/formatting-values-of-numbers-using-text-field/72938/2 "2006-02-26T12:35:26Z")

</div>

On 24 Feb 2006, at 18:08, Hogan, Brian P. wrote:

> means I would have to bring the helper code into the model which  
> I’m not sure I like.

If you mean formatting the field when the user exits the field, you  
should use JavaScript (or write a small JavaScript Helper method, so  
you can easily use it throughout your whole application) :

> **[PageResource.com - All About Web Development](https://pageresource.com)**
>
> PageResource.com is being discontinued as we’re merging with WebsiteSetup.org If you need web development tutorials, follow the links below (from WebsiteSetup.org) HTML Beginner’s Tutorial HTML Cheat Sheet How to Make a Website: Step by Step Guide If...

Best regards

Peter De Berdt

---

<div class="post-metadata">

**Author:** ![HoganS\_Brian\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/9de0a6/32.png) [@HoganS\_Brian\_P](https://www.ruby-forum.com/u/HoganS_Brian_P)\
**Post date:** [February 26, 2006, 12:47pm UTC](https://www.ruby-forum.com/t/formatting-values-of-numbers-using-text-field/72938/3 "2006-02-26T12:47:30Z")

</div>

Hogan, Brian P. wrote:

> I like.  
> Yes, overriding the attribute getter and setter is what I do:

def amount\_s  
‘%.2f’ % amount  
end

def amount\_s= (amount)  
self.amount = amount.to\_f  
end

\<%= text\_field :price, :amount\_s %\>

–  
We develop, watch us RoR, in numbers too big to ignore.
