2 min read

rails has built in number_to_currency helper which takes options like unit, delimeter, seperator which displays foreign currency correctly but somehow it is not best suited for indian currency.

Below is how we managed 2 years ago to display indian currency formatted properly with comma as seperator. personally i think it could be more better than what it is currently ;)

Number to indian currency(rupees) helper

module ApplicationHelper
def number_to_indian_currency(number)
if number
string = number.to_s.split('.')
number = string[0].gsub(/(\d+)(\d{3})$/){ p = $2;"#{$1.reverse.gsub(/(\d{2})/,'\1,').reverse},#{p}"}
number = number.gsub(/^,/, '') + '.' + string[1] if string[1]
# remove leading comma
number = number[1..-1] if number[0] == 44
end
"Rs.#{number}"
end

Sample Output for different combinations

>> helper.number_to_indian_currency(2000)
=> "Rs.2,000"
>> helper.number_to_indian_currency(2040)
=> "Rs.2,040"
>> helper.number_to_indian_currency(2040.50)
=> "Rs.2,040.5"
>> helper.number_to_indian_currency(2040.54)
=> "Rs.2,040.54"
>> helper.number_to_indian_currency(1222040.54)
=> "Rs.12,22,040.54"

After doing google today found from Piyush Ranjan’s Blog that yes there are ways to optimize code.
## Optimized Version

<div class='code-highlight'><pre class='code-highlight-pre'><div data-line='1' class='code-highlight-row numbered'><div class='code-highlight-line'>module ApplicationHelper </div></div><div data-line='2' class='code-highlight-row numbered'><div class='code-highlight-line'> def number_to_indian_currency(number) </div></div><div data-line='3' class='code-highlight-row numbered'><div class='code-highlight-line'> "Rs.#{number.to_s.gsub(/(\d+?)(?=(\d\d)+(\d)(?!\d))(.\d+)?/, "\1,")}" </div></div><div data-line='4' class='code-highlight-row numbered'><div class='code-highlight-line'> end </div></div><div data-line='5' class='code-highlight-row numbered'><div class='code-highlight-line'>end</div></div></pre></div>

Waw one line of code, Look at the beauty of regular expression :) Truely amazing !
## Integrating Webrupee symbol
First include follwing stylesheet in your layout

<div class='code-highlight'><pre class='code-highlight-pre'><div data-line='1' class='code-highlight-row numbered'><div class='code-highlight-line'>//public/stylesheets/font.css </div></div><div data-line='2' class='code-highlight-row numbered'><div class='code-highlight-line'>@font-face { </div></div><div data-line='3' class='code-highlight-row numbered'><div class='code-highlight-line'> font-family: "WebRupee"; </div></div><div data-line='4' class='code-highlight-row numbered'><div class='code-highlight-line'> font-style: normal; </div></div><div data-line='5' class='code-highlight-row numbered'><div class='code-highlight-line'> font-weight: normal; </div></div><div data-line='6' class='code-highlight-row numbered'><div class='code-highlight-line'> src: local("WebRupee"), url("http://cdn.webrupee.com/WebRupee.V2.0.ttf") format("truetype"), url("http://cdn.webrupee.com/WebRupee.V2.0.woff") format("woff"), url("http://cdn.webrupee.com/WebRupee.V2.0.svg") format("svg"); </div></div><div data-line='7' class='code-highlight-row numbered'><div class='code-highlight-line'>} </div></div><div data-line='8' class='code-highlight-row numbered'><div class='code-highlight-line'>.WebRupee { </div></div><div data-line='9' class='code-highlight-row numbered'><div class='code-highlight-line'> font-family: 'WebRupee'; </div></div><div data-line='10' class='code-highlight-row numbered'><div class='code-highlight-line'>}</div></div></pre></div>

Improved Version of Helper

module ApplicationHelper
def number_to_indian_currency(number, html=true)
txt = html ? content_tag(:span, 'Rs.', :class => :WebRupee) : 'Rs.'
"#{txt} #{number.to_s.gsub(/(\d+?)(?=(\d\d)+(\d)(?!\d))(\.\d+)?/, "\\1,")}"
end
end

Usage

>> helper.number_to_indian_currency(400)
=> "<span class="WebRupee">Rs.</span> 400"
>> helper.number_to_indian_currency(5921, false)
=> "Rs. 5,921"
>> helper.number_to_indian_currency(9921)
=> "<span class="WebRupee">Rs.</span> 9,921"

This will show you rupees symbol on your webpages.

Blog Logo

Sandip Ransing


Published

Image

Fun On Rails

Journal of a Web Developer #ruby #rails #JS

Back to Overview