Module: HrLite::AmountInWords
- Defined in:
- lib/hr_lite/amount_in_words.rb
Overview
Indian-system amount in words (crore/lakh/thousand). A PORO — not a view helper — because the slip PDF template is rendered by HOST code (config.render_pdf), where engine helper modules are not in scope.
Constant Summary collapse
- ONES =
%w[zero one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen].freeze
- TENS =
%w[zero ten twenty thirty forty fifty sixty seventy eighty ninety].freeze
- SCALES =
[ [ 10_000_000, "crore" ], [ 100_000, "lakh" ], [ 1000, "thousand" ], [ 100, "hundred" ] ].freeze
Class Method Summary collapse
Class Method Details
.groups(rupees) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/hr_lite/amount_in_words.rb', line 24 def groups(rupees) parts = [] SCALES.each do |divisor, label| next unless rupees >= divisor # The crore group is unbounded — every later group is reduced below # 100 by the preceding modulo, but this one is not, and two_digit # only knows 0..99. Recurse for anything above a hundred crore. count = rupees / divisor spelled = count > 99 ? groups(count).join(" ") : two_digit(count) parts << "#{spelled} #{label}" rupees %= divisor end parts << two_digit(rupees) if rupees.positive? parts end |
.two_digit(number) ⇒ Object
41 42 43 44 45 |
# File 'lib/hr_lite/amount_in_words.rb', line 41 def two_digit(number) return ONES[number] if number < 20 [ TENS[number / 10], number % 10 == 0 ? nil : ONES[number % 10] ].compact.join("-") end |
.words(amount) ⇒ Object
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/hr_lite/amount_in_words.rb', line 13 def words(amount) rupees = Money.round_rupee(amount).to_i return "Zero rupees" if rupees.zero? # A negative amount rendered as bare " rupees": no group was >= its # divisor, so nothing was ever appended. sentence = groups(rupees.abs).join(" ") sentence = "minus #{sentence}" if rupees.negative? "#{sentence.capitalize} rupees" end |