Skip to content

Commit

Permalink
Support python 2 and python 3
Browse files Browse the repository at this point in the history
  • Loading branch information
darland committed Mar 13, 2016
1 parent f76f040 commit 4c6d0c5
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions number_to_text.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# -*- coding: utf8 -*-
'''
Created on 04.07.2011
Changed on 13.03.2016 by Artem Tiumentcev
@author: Sergey Prokhorov <[email protected]>
'''
import decimal
import math


units = (
u'ноль',
Expand Down Expand Up @@ -59,7 +62,7 @@ def thousand(rest, sex):
else:
data = ((teens, 10), (hundreds, 1000))
for names, x in data:
cur = ((rest - prev) % x) * 10 / x
cur = int(((rest - prev) % x) * 10 / x)
prev = rest % x
if x == 10 and use_teens:
plural = 2
Expand All @@ -78,7 +81,7 @@ def thousand(rest, sex):
else:
plural = 2
else:
name.append(names[cur - 1])
name.append(names[cur-1])
return plural, name


Expand All @@ -99,7 +102,7 @@ def num2text(num, main_units=((u'', u'', u''), 'm')):
if nme or ord == 0:
name.append(_orders[ord][0][plural])
name += nme
rest /= 1000
rest = int(rest / 1000)
ord += 1
name.reverse()
return ' '.join(name).strip()
Expand All @@ -108,7 +111,9 @@ def num2text(num, main_units=((u'', u'', u''), 'm')):
def decimal2text(value, places=2,
int_units=(('', '', ''), 'm'),
exp_units=(('', '', ''), 'm')):
value = decimal.Decimal(value)
q = decimal.Decimal(10) ** -places

integral, exp = str(value.quantize(q)).split('.')
return u'{} {}'.format(
num2text(int(integral), int_units),
Expand Down Expand Up @@ -196,13 +201,13 @@ def test_decimal2text(self):
u'сто пять рублей двадцать четыре копейки')
self.assertEqual(
decimal2text(
decimal.Decimal('101.26'),
'101.26',
int_units=int_units,
exp_units=exp_units),
u'сто один рубль двадцать шесть копеек')
self.assertEqual(
decimal2text(
decimal.Decimal('102.2450'),
'102.2450',
places=4,
int_units=int_units,
exp_units=exp_units),
Expand All @@ -227,15 +232,15 @@ def test_decimal2text(self):
try:
num = sys.argv[1]
if '.' in num:
print decimal2text(
print(decimal2text(
decimal.Decimal(num),
int_units=((u'штука', u'штуки', u'штук'), 'f'),
exp_units=((u'кусок', u'куска', u'кусков'), 'm'))
exp_units=((u'кусок', u'куска', u'кусков'), 'm')))
else:
print num2text(
print(num2text(
int(num),
main_units=((u'штука', u'штуки', u'штук'), 'f'))
main_units=((u'штука', u'штуки', u'штук'), 'f')))
except ValueError:
print >> sys.stderr, "Invalid argument {}".format(sys.argv[1])
print (sys.stderr, "Invalid argument {}".format(sys.argv[1]))
sys.exit()
unittest.main()

0 comments on commit 4c6d0c5

Please sign in to comment.