title: A simple approach to currency formatting in Rails
body: <p>I spent most part of today trying to figure out how to give my integer fields a currency format and was bombarded with a myriad of different approaches from using Money, Rails-Money gems to using custom helper methods or locales.</p>
  
  <blockquote>
    <p>You can achieve this by using the <code>number_to_currency</code> method. Say you
    have the following view:</p>
  </blockquote>
  
  <p><em>view.html.erb</em></p>
  
  <pre><code>&lt;ul&gt;
      &lt;li&gt;&lt;%= client.payment %&gt;&lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p><strong>Assuming the column payments is of type integer, this will print out just that, a number with no formatting. Ex: 5523</strong></p>
  
  <blockquote>
    <p>Now add the number_to_currency method and specify which currency unit
    you'd like to use (can be any)</p>
  </blockquote>
  
  <pre><code>&lt;ul&gt;
      &lt;li&gt;&lt;%= number_to_currency(client.payment, :unit =&gt; "$") %&gt;&lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p><strong>Now we get something like this: 5.523.00$</strong></p>
  
  <blockquote>
    <p>The number_to_currency method helper has some options by default, one
    of those is that it inverts (as opposed to common practice) the usage
    of comma and period. These can be modified by adding the options
    :separator and :delimiter and give them the values you'd like as is
    below.</p>
  </blockquote>
  
  <pre><code>&lt;ul&gt;
      &lt;li&gt;&lt;%= number_to_currency(client.payment, :unit =&gt; "$", :separator =&gt; ".", :delimiter =&gt; ",") %&gt;&lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p>These are the available options for the number_to_currency method helper (<a href="http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html" rel="noreferrer">RubyOnRailsAPI</a>):</p>
  
  <pre><code>:locale - Sets the locale to be used for formatting (defaults to current locale).
  
  :precision - Sets the level of precision (defaults to 2).
  
  :unit - Sets the denomination of the currency (defaults to “$”).
  
  :separator - Sets the separator between the units (defaults to “.”).
  
  :delimiter - Sets the thousands delimiter (defaults to “,”).
  
  :format - Sets the format for non-negative numbers (defaults to “%u%n”). Fields are %u for the currency, and %n for the number.
  
  :negative_format - Sets the format for negative numbers (defaults to prepending a hyphen to the formatted number given by :format). Accepts the same fields than :format, except %n is here the absolute value of the number.
  
  :raise - If true, raises InvalidNumberError when the argument is invalid.
  </code></pre>