Skip to content

Commit ede0c42

Browse files
authored
Merge branch 'master' into en_coverage
2 parents 3d14f7a + e674ce7 commit ede0c42

File tree

3 files changed

+81
-41
lines changed

3 files changed

+81
-41
lines changed

num2words/lang_ES.py

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
1616
# MA 02110-1301 USA
1717

18-
from __future__ import print_function, unicode_literals
18+
from __future__ import division, print_function, unicode_literals
19+
20+
import math
1921

2022
from .lang_EU import Num2Word_EU
2123

@@ -120,47 +122,47 @@ def merge(self, curr, next):
120122

121123
def to_ordinal(self, value):
122124
self.verify_ordinal(value)
123-
try:
124-
if value == 0:
125-
text = ""
126-
elif value <= 10:
127-
text = "%s%s" % (self.ords[value], self.gender_stem)
128-
elif value <= 12:
129-
text = (
130-
"%s%s%s" % (self.ords[10], self.gender_stem,
131-
self.to_ordinal(value - 10))
132-
)
133-
elif value <= 100:
134-
dec = (value // 10) * 10
135-
text = (
136-
"%s%s %s" % (self.ords[dec], self.gender_stem,
137-
self.to_ordinal(value - dec))
138-
)
139-
elif value <= 1e3:
140-
cen = (value // 100) * 100
141-
text = (
142-
"%s%s %s" % (self.ords[cen], self.gender_stem,
143-
self.to_ordinal(value - cen))
144-
)
145-
elif value < 1e18:
146-
# dec contains the following:
147-
# [ 1e3, 1e6): 1e3
148-
# [ 1e6, 1e9): 1e6
149-
# [ 1e9, 1e12): 1e9
150-
# [1e12, 1e15): 1e12
151-
# [1e15, 1e18): 1e15
152-
dec = 10 ** ((((len(str(int(value))) - 1) / 3 - 1) + 1) * 3)
153-
part = int(float(value / dec) * dec)
154-
cardinal = (
155-
self.to_cardinal(part / dec) if part / dec != 1 else ""
125+
if value == 0:
126+
text = ""
127+
elif value <= 10:
128+
text = "%s%s" % (self.ords[value], self.gender_stem)
129+
elif value <= 12:
130+
text = (
131+
"%s%s%s" % (self.ords[10], self.gender_stem,
132+
self.to_ordinal(value - 10))
133+
)
134+
elif value <= 100:
135+
dec = (value // 10) * 10
136+
text = (
137+
"%s%s %s" % (self.ords[dec], self.gender_stem,
138+
self.to_ordinal(value - dec))
156139
)
157-
text = (
158-
"%s%s%s %s" % (cardinal, self.ords[dec], self.gender_stem,
159-
self.to_ordinal(value - part))
160-
)
161-
else:
162-
text = self.to_cardinal(value)
163-
except KeyError:
140+
elif value <= 1e3:
141+
cen = (value // 100) * 100
142+
text = (
143+
"%s%s %s" % (self.ords[cen], self.gender_stem,
144+
self.to_ordinal(value - cen))
145+
)
146+
elif value < 1e18:
147+
# Round down to the nearest 1e(3n)
148+
# dec contains the following:
149+
# [ 1e3, 1e6): 1e3
150+
# [ 1e6, 1e9): 1e6
151+
# [ 1e9, 1e12): 1e9
152+
# [1e12, 1e15): 1e12
153+
# [1e15, 1e18): 1e15
154+
dec = 1000 ** int(math.log(int(value), 1000))
155+
156+
# Split the parts before and after the word for 'dec'
157+
# eg (12, 345) = divmod(12_345, 1_000)
158+
high_part, low_part = divmod(value, dec)
159+
160+
cardinal = self.to_cardinal(high_part) if high_part != 1 else ""
161+
text = (
162+
"%s%s%s %s" % (cardinal, self.ords[dec], self.gender_stem,
163+
self.to_ordinal(low_part))
164+
)
165+
else:
164166
text = self.to_cardinal(value)
165167
return text.strip()
166168

tests/test_dk.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# coding: utf-8
2+
# Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
3+
# Copyright (c) 2013, Savoir-faire Linux inc. All Rights Reserved.
4+
5+
# This library is free software; you can redistribute it and/or
6+
# modify it under the terms of the GNU Lesser General Public
7+
# License as published by the Free Software Foundation; either
8+
# version 2.1 of the License, or (at your option) any later version.
9+
# This library is distributed in the hope that it will be useful,
10+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
# Lesser General Public License for more details.
13+
# You should have received a copy of the GNU Lesser General Public
14+
# License along with this library; if not, write to the Free Software
15+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
16+
# MA 02110-1301 USA
17+
18+
from __future__ import unicode_literals
19+
20+
from unittest import TestCase
21+
22+
from num2words import num2words
23+
24+
25+
class Num2WordsDKTest(TestCase):
26+
def test_ordinal(self):
27+
self.assertEqual(num2words(1, to="ordinal", lang="dk"), "første")
28+
self.assertEqual(num2words(5, to="ordinal", lang="dk"), "femte")
29+
30+
def test_cardinal(self):
31+
self.assertEqual(num2words(0, to="cardinal", lang="dk"), "nul")
32+
self.assertEqual(num2words(1, to="cardinal", lang="dk"), "et")
33+
self.assertEqual(num2words(2, to="cardinal", lang="dk"), "to")
34+
self.assertEqual(num2words(5, to="cardinal", lang="dk"), "fem")
35+
self.assertEqual(num2words(8, to="cardinal", lang="dk"), "otte")
36+
self.assertEqual(num2words(18, to="cardinal", lang="dk"), "atten")
37+
self.assertEqual(num2words(45, to="cardinal", lang="dk"), "femogfyrre")

tests/test_es.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
(28, 'vigésimo octavo'),
9696
(100, 'centésimo'),
9797
(1000, 'milésimo'),
98+
(12345, 'docemilésimo tricentésimo quadragésimo quinto'),
9899
(1000000, 'millonésimo'),
99100
(1000000000000000, 'cuadrillonésimo'),
100101
(1000000000000000000, 'un trillón') # over 1e18 is not supported

0 commit comments

Comments
 (0)