mirror of
https://github.com/bblaz/num2words.git
synced 2025-12-06 06:42:25 +00:00
Fix bugs in the float precision and the currency, increase test coverage (#134)
* Fix some bugs in the float precision and the currency, increase test coverage. Ref: issue #112 #113 * Update README.rst
This commit is contained in:
committed by
GitHub
parent
5a131fedc6
commit
1c699d1bb4
@@ -73,7 +73,7 @@ CONVERTER_CLASSES = {
|
||||
}
|
||||
|
||||
|
||||
CONVERTES_TYPES = ['cardinal', 'ordinal', 'year', 'currency']
|
||||
CONVERTES_TYPES = ['cardinal', 'ordinal', 'ordinal_num', 'year', 'currency']
|
||||
|
||||
|
||||
def num2words(number, ordinal=False, lang='en', to='cardinal', **kwargs):
|
||||
|
||||
@@ -18,6 +18,7 @@ from __future__ import unicode_literals
|
||||
|
||||
import math
|
||||
from collections import OrderedDict
|
||||
from decimal import Decimal
|
||||
|
||||
from .compat import to_s
|
||||
|
||||
@@ -106,6 +107,10 @@ class Num2Word_Base(object):
|
||||
|
||||
def float2tuple(self, value):
|
||||
pre = int(value)
|
||||
|
||||
# Simple way of finding decimal places to update the precision
|
||||
self.precision = abs(Decimal(str(value)).as_tuple().exponent)
|
||||
|
||||
post = abs(value - pre) * 10**self.precision
|
||||
if abs(round(post) - post) < 0.01:
|
||||
# We generally floor all values beyond our precision (rather than
|
||||
|
||||
@@ -91,7 +91,7 @@ class Num2Word_ES(Num2Word_EU):
|
||||
if nnum < 1000000:
|
||||
return next
|
||||
ctext = "un"
|
||||
elif cnum == 100 and not nnum == 1000:
|
||||
elif cnum == 100 and not nnum % 1000 == 0:
|
||||
ctext += "t" + self.gender_stem
|
||||
|
||||
if nnum < cnum:
|
||||
@@ -117,7 +117,6 @@ class Num2Word_ES(Num2Word_EU):
|
||||
|
||||
def to_ordinal(self, value):
|
||||
self.verify_ordinal(value)
|
||||
text = ""
|
||||
try:
|
||||
if value == 0:
|
||||
text = ""
|
||||
@@ -167,31 +166,10 @@ class Num2Word_ES(Num2Word_EU):
|
||||
return "%s%s" % (value, "º" if self.gender_stem == 'o' else "ª")
|
||||
|
||||
def to_currency(self, val, longval=True, old=False):
|
||||
hightxt, lowtxt = "euro/s", "centavo/s"
|
||||
if old:
|
||||
return self.to_splitnum(val, hightxt="peso/s", lowtxt="peseta/s",
|
||||
divisor=1000, jointxt="y", longval=longval)
|
||||
return super(Num2Word_ES, self).to_currency(val, jointxt="y",
|
||||
longval=longval)
|
||||
|
||||
|
||||
n2w = Num2Word_ES()
|
||||
to_card = n2w.to_cardinal
|
||||
to_ord = n2w.to_ordinal
|
||||
to_ordnum = n2w.to_ordinal_num
|
||||
|
||||
|
||||
def main():
|
||||
for val in [1, 11, 12, 21, 31, 33, 71, 80, 81, 91, 99, 100, 101, 102, 155,
|
||||
180, 300, 308, 832, 1000, 1001, 1061, 1100, 1500, 1701, 3000,
|
||||
8280, 8291, 150000, 500000, 1000000, 2000000, 2000001,
|
||||
-21212121211221211111, -2.121212, -1.0000100]:
|
||||
n2w.test(val)
|
||||
|
||||
n2w.test(13253254360678768017687001076010010122121321432104732075403270573)
|
||||
print(n2w.to_currency(1222))
|
||||
print(n2w.to_currency(1222, old=True))
|
||||
print(n2w.to_year(1222))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
hightxt, lowtxt = "peso/s", "peseta/s"
|
||||
result = self.to_splitnum(val, hightxt=hightxt, lowtxt=lowtxt,
|
||||
divisor=1, jointxt="y", longval=longval)
|
||||
# Handle exception, in spanish is "un euro" and not "uno euro"
|
||||
return result.replace("uno", "un")
|
||||
|
||||
@@ -24,28 +24,7 @@ from .lang_ES import Num2Word_ES
|
||||
class Num2Word_ES_CO(Num2Word_ES):
|
||||
|
||||
def to_currency(self, val, longval=True, old=False):
|
||||
return self.to_splitnum(val, hightxt="peso/s", lowtxt="peso/s",
|
||||
divisor=1000, jointxt="y", longval=longval)
|
||||
|
||||
|
||||
n2w = Num2Word_ES_CO()
|
||||
to_card = n2w.to_cardinal
|
||||
to_ord = n2w.to_ordinal
|
||||
to_ordnum = n2w.to_ordinal_num
|
||||
|
||||
|
||||
def main():
|
||||
for val in [1, 11, 12, 21, 31, 33, 71, 80, 81, 91, 99, 100, 101, 102, 155,
|
||||
180, 300, 308, 832, 1000, 1001, 1061, 1100, 1500, 1701, 3000,
|
||||
8280, 8291, 150000, 500000, 1000000, 2000000, 2000001,
|
||||
-21212121211221211111, -2.121212, -1.0000100]:
|
||||
n2w.test(val)
|
||||
|
||||
n2w.test(13253254360678768017687001076010010122121321432104732075403270573)
|
||||
print(n2w.to_currency(1222))
|
||||
print(n2w.to_currency(1222, old=True))
|
||||
print(n2w.to_year(1222))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
result = self.to_splitnum(val, hightxt="peso/s", lowtxt="centavo/s",
|
||||
divisor=1, jointxt="y", longval=longval)
|
||||
# Handle exception, in spanish is "un euro" and not "uno euro"
|
||||
return result.replace("uno", "un")
|
||||
|
||||
@@ -24,29 +24,10 @@ from .lang_ES import Num2Word_ES
|
||||
class Num2Word_ES_VE(Num2Word_ES):
|
||||
|
||||
def to_currency(self, val, longval=True, old=False):
|
||||
return self.to_splitnum(val, hightxt="bolívar/es Fuerte/s",
|
||||
lowtxt="bolívar/es fuerte/s",
|
||||
divisor=1000, jointxt="y", longval=longval)
|
||||
|
||||
|
||||
n2w = Num2Word_ES_VE()
|
||||
to_card = n2w.to_cardinal
|
||||
to_ord = n2w.to_ordinal
|
||||
to_ordnum = n2w.to_ordinal_num
|
||||
|
||||
|
||||
def main():
|
||||
for val in [1, 11, 12, 21, 31, 33, 71, 80, 81, 91, 99, 100, 101, 102, 155,
|
||||
180, 300, 308, 832, 1000, 1001, 1061, 1100, 1500, 1701, 3000,
|
||||
8280, 8291, 150000, 500000, 1000000, 2000000, 2000001,
|
||||
-21212121211221211111, -2.121212, -1.0000100]:
|
||||
n2w.test(val)
|
||||
|
||||
n2w.test(13253254360678768017687001076010010122121321432104732075403270573)
|
||||
print(n2w.to_currency(1222))
|
||||
print(n2w.to_currency(1222, old=True))
|
||||
print(n2w.to_year(1222))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
hightxt = "bolívar/es" if old else "bolívar/es fuerte/s"
|
||||
result = self.to_splitnum(
|
||||
val, hightxt=hightxt, lowtxt="centavo/s",
|
||||
divisor=1, jointxt="y", longval=longval
|
||||
)
|
||||
# Handle exception, in spanish is "un euro" and not "uno euro"
|
||||
return result.replace("uno", "un")
|
||||
|
||||
Reference in New Issue
Block a user