# Number\_to\_currency() in the view?

**URL:** <https://www.ruby-forum.com/t/number-to-currency-in-the-view/167033>\
**Category:** Rails\
**Created:** [May 2, 2009, 2:44pm UTC](https://www.ruby-forum.com/t/number-to-currency-in-the-view/167033 "2009-05-02T14:44:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [May 2, 2009, 2:44pm UTC](https://www.ruby-forum.com/t/number-to-currency-in-the-view/167033/1 "2009-05-02T14:44:04Z")

</div>

On p. 93, AWDWR(3rd) it says,

* * *

…we could do this:

\<%= sprintf("$0.02f", product.price) %

## This would work, but it embeds knowledge of currency formatting into the view. Should we want to internationalize the application later, this would be a maintenance problem.

\<Goody. Let’s move the formatting out of the view and into a more  
centralized location. The Product model? Hmm…maybe, but not all  
calls to the Product model will want the number converted to a formatted  
string. The ProductsController then? Yes, yes. That must be it!\>

* * *

## Instead, let’s use a helper method to format the price as a currency. … … \<%= number\_to\_currency(product.price) %

Wt?? How does that solve any maintenance issues. If I later want to  
change the view to display a different format, what’s the difference  
between having to change the parameters of number\_to\_currency()  
everywhere it appears vs. changing the format of sprintf()?

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [May 2, 2009, 4:28pm UTC](https://www.ruby-forum.com/t/number-to-currency-in-the-view/167033/2 "2009-05-02T16:28:05Z")

</div>

On May 2, 1:44 pm, 7stud – [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## … \<%= number\_to\_currency(product.price) %
> 
> Wt?? How does that solve any maintenance issues. If I later want to  
> change the view to display a different format, what’s the difference  
> between having to change the parameters of number\_to\_currency()  
> everywhere it appears vs. changing the format of sprintf()?

Well rather than having format strings spread all over the place, you  
centralized the concept of formatting a number in one place, the  
number\_to\_currency method. If you wanted to change currency formatting  
you’d just have to change that one helper.

Fred

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [May 3, 2009, 1:15am UTC](https://www.ruby-forum.com/t/number-to-currency-in-the-view/167033/3 "2009-05-03T01:15:14Z")

</div>

Frederick C. wrote:

> On May 2, 1:44ï¿½pm, 7stud – [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > ## … \<%= number\_to\_currency(product.price) %
> > 
> > Wt?? ï¿½How does that solve any maintenance issues. ï¿½If I later want to  
> > change the view to display a different format, what’s the difference  
> > between having to change the parameters of number\_to\_currency()  
> > everywhere it appears vs. changing the format of sprintf()?
> 
> Well rather than having format strings spread all over the place, you  
> centralized the concept of formatting a number in one place, the  
> number\_to\_currency method. If you wanted to change currency formatting  
> you’d just have to change that one helper.

How do you override a helper method?

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [May 3, 2009, 11:41am UTC](https://www.ruby-forum.com/t/number-to-currency-in-the-view/167033/4 "2009-05-03T11:41:34Z")

</div>

On May 3, 12:15 am, 7stud – [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> How do you override a helper method?

Same as any other method ? (although I suppose helper :all might throw  
a slight spanner in the works)

Fred
