5 ms·
Show HN: Indian Rupee formatting and word conversion for Ruby
- aboutruby 8y ago# Paisa.format / Paisa.format_with_sym This can be done with Rails's number_to_currency helper: https://apidock.com/rails/ActionView/Helpers/NumberHelper/number_to_currency https://apidock.com/rails/ActionView/Helpers/NumberHelper/nu... # Paisa.words There is number_to_human which is close: https://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html#method-i-number_to_human https://api.rubyonrails.org/classes/ActionView/Helpers/Numbe..., otherwise number_to_words's gem https://github.com/kslazarev/numbers_and_words https://github.com/kslazarev/numbers_and_words I would actually recommend using the money gem to handle money objects: https://github.com/RubyMoney/money https://github.com/RubyMoney/money
- sudhirj 8y agoAll of the methods and libraries assume regular grouping, but India's money is grouped irregularly. While regular money is counted in groups of 10^3 (thousands, millions, billions, trillions), in India there's a 3,2,2 cycle. One lakhs = 100,000 and 1 crore is 100 lakhs, but if you had 1000 of them it would still be a thousand. Everybody seems to get this wrong, even Google Sheets.
- marvindanig 8y agoTremendous, thanks for sharing this gem!
- aboutruby 8y agoOh I see, sorry, didn't look closely enough at the examples. Great project! Maybe this could be upstreamed in Rails?
- sudhirj 8y agoYeah, that would be ideal. Let me get the algos tightened up with feedback and testing, and I’ll try submitting them.
- viraptor 8y agohttps://github.com/RubyMoney/money https://github.com/RubyMoney/money should cover the formatting for this and many other currencies as well.
- sudhirj 8y agoI’m not seeing support for irregular cycles - in all these libs you can customise the 10^3 separator and the decimal mark, but the problem in India is that the separators follow a 10^3,10^2,10^2 repeating separator cycle.
- viraptor 8y agoMoney.new(10000000, "INR").format(south_asian_number_formatting: true) #=> "1,00,000.00" If there are any other cases missing, I'm sure they'd be interested to know.
- sudhirj 8y agoAh, thanks. Never thought of searching for "south asian". Let me see if that covers all the cases, will either deprecate or use the Money gem internally.
- kkarakk 8y ago2017 tag please
- aliswe 8y agoThis is amazing.
- mtmail 8y agoAges ago we wrote something similar for Perl https://metacpan.org/pod/Number::Format::SouthAsian https://metacpan.org/pod/Number::Format::SouthAsian supporting the https://en.wikipedia.org/wiki/Indian_numbering_system https://en.wikipedia.org/wiki/Indian_numbering_system
- lappet 8y agovery cool