Forum: Ruby on Rails Formatting values of numbers using text_field

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
60e38de043848f82392062088f191213?d=identicon&s=25 Hogan, Brian P. (Guest)
on 2006-02-24 18:09
(Received via mailing list)
Anyone have a quick solution on how to format a number in a text_field

<% 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 Hogan
Web Development
Learning & Technology Services
Schofield 3-B
University of Wisconsin-Eau Claire
715 836 3585
00973881979aa0a660ffbbb2f7a907fb?d=identicon&s=25 Peter De Berdt (Guest)
on 2006-02-26 12:35
(Received via mailing list)
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) :

Best regards

Peter De Berdt
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2006-02-26 12:47
(Received via mailing list)
Hogan, Brian P. wrote:
> I like.
Yes, overriding the attribute getter and setter is what I do:

def amount_s
   '%.2f' % amount

def amount_s= (amount)
   self.amount = amount.to_f

<%= text_field :price, :amount_s %>

We develop, watch us RoR, in numbers too big to ignore.
This topic is locked and can not be replied to.