Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Hindi Script Support #2

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 60 additions & 2 deletions lib/paisa.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,45 @@ def self.tens_hash(tens_name)
0 => ONES
}.freeze

HINDI_MAPPING = {
one:'एक', two: 'दो', three: 'तीन', four: 'चार', five: 'पांच', six: 'छह', seven: 'सात', zero: 'शून्य',
eight: 'आठ', nine: 'नौ', ten: 'दस', eleven: 'ग्यारह', twelve: 'बारह', thirteen: 'तेरह', fourteen: 'चौदह', fifteen: 'पंद्रह',
sixteen: 'सोलह', seventeen: 'सत्रह', eighteen: 'अठारह', 'nineteen': 'उन्नीस', hundred: 'सौ', thousand: 'हजार',
lakh: 'लाख', crore: 'करोड़', rupees: 'रुपये', paise: 'पैसे', and: 'और',
twenty: {
one: 'इकीस', two: 'बाईस', three: 'तेइस', four: 'चौबीस', five: 'पच्चीस', six: 'छब्बीस', seven: 'सताइस',
eight: 'अट्ठाइस', nine: 'उनतीस', zero: 'बीस'
},
thirty: {
one: 'इकतीस', two: 'बतीस', three: 'तैंतीस', four: 'चौंतीस', five: 'पैंतीस', six: 'छतीस', seven: 'सैंतीस', eight: 'अड़तीस',
nine: 'उनतालीस', zero: 'तीस'
},
forty: {
one: 'इकतालीस', two: 'बयालीस', three: 'तैतालीस', four: 'चवालीस', five: 'पैंतालीस', six: 'छयालिस', seven: 'सैंतालीस',
eight: 'अड़तालीस', nine: 'उनचास', zero: 'चालीस'
},
fifty: {
one: 'इक्यावन', two: 'बावन', three: 'तिरपन', four: 'चौवन', five: 'पचपन', six: 'छप्पन', seven: 'सतावन', eight: 'अठावन',
nine: 'उनसठ', zero: 'पचास'
},
sixty: {
one: 'इकसठ', two: 'बासठ', three: 'तिरसठ', four: 'चौंसठ', five: 'पैंसठ', six: 'छियासठ', seven: 'सड़सठ',
eight: 'अड़सठ', nine: 'उनहतर', zero: 'साठ'
},
seventy: {
one: 'इकहतर', two: 'बहतर', three: 'तिहतर', four: 'चौहतर', five: 'पचहतर', six: 'छिहतर', seven: 'सतहतर',
eight: 'अठहतर', nine: 'उन्नासी', zero: 'सत्तर'
},
eighty: {
one: 'इक्यासी', two: 'बयासी', three: 'तिरासी', four: 'चौरासी', five: 'पचासी', six: 'छियासी', seven: 'सतासी',
eight: 'अट्ठासी', nine: 'नवासी', zero: 'अस्सी'
},
ninety: {
one: 'इक्यानवे', two: 'बानवे', three: 'तिरानवे', four: 'चौरानवे', five: 'पचानवे', six: 'छियानवे', seven: 'सतानवे',
eight: 'अट्ठानवे', nine: 'निन्यानवे', zero: 'नब्बे'
}
}.freeze

def self.format(paise, precision: 2)
rupee_parts, paise_part = parse(paise)
[
Expand All @@ -45,7 +84,7 @@ def self.format_with_sym(paise, precision: 2)
[SYMBOL, format(paise, precision: precision)].join
end

def self.words(paise)
def self.words(paise, lang="english")
maknahar marked this conversation as resolved.
Show resolved Hide resolved
rupee_parts, paise_part = parse(paise)
rupee_text_parts = rupee_parts.reverse.each_with_object([]) do |section, memo|
label = ['', ' thousand', ' lakh', ' crore'][memo.size]
Expand All @@ -58,7 +97,26 @@ def self.words(paise)
text = []
text << [rupee_text_parts.to_a.compact.reverse.join(', '), 'rupees'].join(' ') unless rupee_text_parts.empty?
text << [to_words(paise_part), 'paise'].join(' ') unless paise_part.to_i.zero?
text.join(', ')
case lang
when "hindi"
return hindi(text.join(', '))
else
return text.join(', ')
end
end

def self.hindi(english)
h = nil
english.split(', ').map { | x | x.split.map do | k |
maknahar marked this conversation as resolved.
Show resolved Hide resolved
if HINDI_MAPPING[k.to_sym].is_a? Hash
h = HINDI_MAPPING[k.to_sym]
else
hindi_number = h.nil? ? HINDI_MAPPING[k.to_sym] : (h[k.to_sym].nil? ? [h[:zero], HINDI_MAPPING[k.to_sym]].join(' ') : h[k.to_sym])
h = nil
end
hindi_number
end.compact.join(' ')
}.join(', ')
end

def self.parse(paise)
Expand Down
27 changes: 27 additions & 0 deletions test/paisa_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,31 @@ def test_words
assert_equal 'five crore, forty two lakh rupees', Paisa.words(5420000000)
assert_equal 'five crore, forty two lakh rupees, seventy four paise', Paisa.words(5420000074)
end

def test_words_hindi
assert_equal 'पांच करोड़, दो लाख रुपये, चार पैसे', Paisa.words(5020000004, "hindi")
assert_equal 'पांच करोड़, दो लाख रुपये, चौदह पैसे', Paisa.words(5020000014, "hindi")
assert_equal 'बयालीस पैसे', Paisa.words(42, "hindi")
assert_equal "दो पैसे", Paisa.words(2,"hindi")
assert_equal 'दस पैसे', Paisa.words(10, "hindi")
assert_equal 'पंद्रह पैसे', Paisa.words(15, "hindi")
assert_equal 'बानवे पैसे', Paisa.words(92, "hindi")
assert_equal 'दो रुपये, बानवे पैसे', Paisa.words(292, "hindi")
assert_equal 'अट्ठाइस रुपये, सैंतीस पैसे', Paisa.words(2837, "hindi")
assert_equal 'नौ सौ और तेइस रुपये, अड़तालीस पैसे', Paisa.words(92348, "hindi")
assert_equal 'आठ हजार, चार सौ और बासठ रुपये, सताइस पैसे', Paisa.words(846227, "hindi")
assert_equal 'सतावन हजार, दो सौ और दो रुपये, चौहतर पैसे', Paisa.words(5720274, "hindi")
assert_equal 'सात लाख, पचहतर हजार, एक सौ और तिरानवे रुपये, पचासी पैसे', Paisa.words(77519385, "hindi")
assert_equal 'सड़सठ लाख, पचहतर हजार, एक सौ और तिरानवे रुपये, पचासी पैसे', Paisa.words(677519385, "hindi")
assert_equal 'आठ करोड़, सड़सठ लाख, पचहतर हजार, एक सौ और तिरानवे रुपये, पचासी पैसे', Paisa.words(8677519385, "hindi")
assert_equal 'अठारह करोड़, सड़सठ लाख, पचहतर हजार, एक सौ और तिरानवे रुपये, पचासी पैसे', Paisa.words(18677519385, "hindi")
assert_equal 'सात सौ और अठारह करोड़, सड़सठ लाख, पचहतर हजार, एक सौ और तिरानवे रुपये, पचासी पैसे', Paisa.words(718677519385, "hindi")
assert_equal 'तीन सौ रुपये', Paisa.words(300_00, "hindi")
assert_equal 'पचास हजार रुपये', Paisa.words(5000000, "hindi")
assert_equal 'पांच लाख रुपये', Paisa.words(50000000, "hindi")
assert_equal 'पांच करोड़ रुपये', Paisa.words(5000000000, "hindi")
assert_equal 'पांच करोड़, बयालीस लाख रुपये', Paisa.words(5420000000, "hindi")
assert_equal 'पांच करोड़, बयालीस लाख रुपये, चौहतर पैसे', Paisa.words(5420000074, "hindi")
end

end