Skip to content

Commit

Permalink
add to_ordinal method
Browse files Browse the repository at this point in the history
  • Loading branch information
ZendaInnocent committed Sep 27, 2023
1 parent edb3aa1 commit 44728f2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
30 changes: 26 additions & 4 deletions num2words/lang_SW.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,32 @@ def thousands_to_cardinal(self, number):
return f'{ONES[thousands]} elfu na {self.tens_to_cardinal(remainder)}'
elif thousands < 100:
# thousands = 10 - 99
return f'{self.tens_to_cardinal(thousands)} elfu na {self.tens_to_cardinal(remainder)}'
return (
f'{self.tens_to_cardinal(thousands)} elfu na '
f'{self.tens_to_cardinal(remainder)}'
)
elif thousands < 1000:
# thousands = 100 - 999
return f'{self.hundreds_to_cardinal(thousands)} elfu, {self.tens_to_cardinal(remainder)}'
return (
f'{self.hundreds_to_cardinal(thousands)} elfu, '
f'{self.tens_to_cardinal(remainder)}'
)
elif remainder < 1000:
if thousands < 10:
# thousands = 1 - 9
return f'{ONES[thousands]} elfu, {self.hundreds_to_cardinal(remainder)}'
elif thousands < 100:
# thousands = 10 - 99
return f'{self.tens_to_cardinal(thousands)} elfu, {self.hundreds_to_cardinal(remainder)}'
return (
f'{self.tens_to_cardinal(thousands)} elfu, '
f'{self.hundreds_to_cardinal(remainder)}'
)
else:
# thousands = 100 - 999
return f'{self.hundreds_to_cardinal(thousands)} elfu, {self.hundreds_to_cardinal(remainder)}'
return (
f'{self.hundreds_to_cardinal(thousands)} elfu, '
f'{self.hundreds_to_cardinal(remainder)}'
)

def big_number_to_cardinal(self, number):
digits = [c for c in str(number)]
Expand Down Expand Up @@ -180,3 +192,13 @@ def to_cardinal(self, number):
# 1_000_000 - Infinity
string = self.big_number_to_cardinal(int(number))
return string

def to_ordinal(self, number):
'''Converts number to ordinal.'''
if number <= 0:
raise Exception('number must be greater than zero to convert to ordinal')
if number == 1:
return 'kwanza'
if number == 2:
return 'pili'
return self.to_cardinal(number)
19 changes: 16 additions & 3 deletions tests/test_sw.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,26 @@ def test_big_numbers(self):
)
self.assertEqual(
num2words(911_581_317, lang='sw'),
'mia tisa kumi na moja milioni, mia tano themanini na moja elfu, mia tatu kumi na saba',
'mia tisa kumi na moja milioni, mia tano themanini na moja elfu, '
'mia tatu kumi na saba',
)
self.assertEqual(
num2words(1_911_581_317, lang='sw'),
'moja bilioni, mia tisa kumi na moja milioni, mia tano themanini na moja elfu, mia tatu kumi na saba',
'moja bilioni, mia tisa kumi na moja milioni, mia tano themanini '
'na moja elfu, mia tatu kumi na saba',
)
self.assertEqual(
num2words(882_911_581_317, lang='sw'),
'mia nane themanini na mbili bilioni, mia tisa kumi na moja milioni, mia tano themanini na moja elfu, mia tatu kumi na saba',
'mia nane themanini na mbili bilioni, mia tisa kumi na moja '
'milioni, mia tano themanini na moja elfu, mia tatu kumi na saba',
)

def test_number_to_ordinal(self):
self.assertEqual(num2words(1, ordinal=True, lang='sw'), 'kwanza')
self.assertEqual(num2words(2, ordinal=True, lang='sw'), 'pili')
self.assertEqual(num2words(3, ordinal=True, lang='sw'), 'tatu')
self.assertEqual(num2words(9, ordinal=True, lang='sw'), 'tisa')
self.assertEqual(num2words(12, ordinal=True, lang='sw'), 'kumi na mbili')
self.assertRaises(
Exception, 'number must be greater than zero to convert to ordinal'
)

0 comments on commit 44728f2

Please sign in to comment.