How to separate a number according to the Indian numbering system in Rails?

717 Views Asked by At

I have used number_with_delimiter method for adding commas for numbers in an invoice in Ruby on Rails. But the number format resulted in 23,324,455 and not as 2,33,24,455 that is, Indian Rupees format.

<%= number_with_precision(@number, :precision => 2, :delimiter => ',') %>

I have to generate Invoice with amount in Rupees so the format should be xx,xx,xxx.00. Is it possible in Rails? How to accomplish it?

This can be done with JavaScript but the problem is, I have generated the invoice in PDF format using PDFKit gem which does not respond with JavaScript. I have used the required js code on loading the document.

$(document).ready(function(){
  $('.totalvalue').each(function(){
    value = $('.totalvalue').text();
    $('.totalvalue').html(
      (value+"").split("").reverse().join("").replace(/(\d{3})(?=\d)/g,        '$1,').split("").reverse().join("")
    )
  })
})
2

There are 2 best solutions below

2
Wand Maker On BEST ANSWER

You can use money gem, and specifically explore its formatting options.

Money.new(10000000, "INR").format(:symbol => false, 
                                  :south_asian_number_formatting => true)    
#=> "1,00,000.00"
1
Amadan On

Not elegant, but works:

("%.2f" % 23324455).reverse.gsub(/(?<=.{6})(\d\d?)/, ',\1').reverse
# => "2,33,24,455.00"