1
# -*- coding: utf-8 -*-
2
##############################################################################
4
# OpenERP, Open Source Management Solution
5
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
7
# This program is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU Affero General Public License as
9
# published by the Free Software Foundation, either version 3 of the
10
# License, or (at your option) any later version.
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU Affero General Public License for more details.
17
# You should have received a copy of the GNU Affero General Public License
18
# along with this program. If not, see <http://www.gnu.org/licenses/>.
20
##############################################################################
23
from translate import _
25
_logger = logging.getLogger(__name__)
27
#-------------------------------------------------------------
29
#-------------------------------------------------------------
31
to_19 = ( 'Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six',
32
'Seven', 'Eight', 'Nine', 'Ten', 'Eleven', 'Twelve', 'Thirteen',
33
'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen' )
34
tens = ( 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety')
36
'Thousand', 'Million', 'Billion', 'Trillion', 'Quadrillion',
37
'Quintillion', 'Sextillion', 'Septillion', 'Octillion', 'Nonillion',
38
'Decillion', 'Undecillion', 'Duodecillion', 'Tredecillion', 'Quattuordecillion',
39
'Sexdecillion', 'Septendecillion', 'Octodecillion', 'Novemdecillion', 'Vigintillion' )
42
"""convert a value < 100 to English.
46
for (dcap, dval) in ((k, 20 + (10 * v)) for (v, k) in enumerate(tens)):
49
return dcap + '-' + to_19[val % 10]
52
def _convert_nnn(val):
54
convert a value < 1000 to english, special cased because it is the level that kicks
55
off the < 100 special case. The rest are more general. This also allows you to
56
get strings in the form of 'forty-five hundred' if called directly.
59
(mod, rem) = (val % 100, val // 100)
61
word = to_19[rem] + ' Hundred'
65
word = word + _convert_nn(mod)
68
def english_number(val):
70
return _convert_nn(val)
72
return _convert_nnn(val)
73
for (didx, dval) in ((v - 1, 1000 ** v) for v in range(len(denom))):
78
ret = _convert_nnn(l) + ' ' + denom[didx]
80
ret = ret + ', ' + english_number(r)
83
def amount_to_text(number, currency):
84
number = '%.2f' % number
86
list = str(number).split('.')
87
start_word = english_number(int(list[0]))
88
end_word = english_number(int(list[1]))
89
cents_number = int(list[1])
90
cents_name = (cents_number > 1) and 'Cents' or 'Cent'
91
final_result = start_word +' '+units_name+' and ' + end_word +' '+cents_name
95
#-------------------------------------------------------------
97
#-------------------------------------------------------------
99
_translate_funcs = {'en' : amount_to_text}
101
#TODO: we should use the country AND language (ex: septante VS soixante dix)
102
#TODO: we should use en by default, but the translation func is yet to be implemented
103
def amount_to_text(nbr, lang='en', currency='euro'):
104
""" Converts an integer to its textual representation, using the language set in the context if any.
108
1654: thousands six cent cinquante-quatre.
110
import openerp.loglevels as loglevels
112
# _logger.warning(_("Number too large '%d', can not translate it"))
115
if not _translate_funcs.has_key(lang):
116
_logger.warning(_("no translation function found for lang: '%s'"), lang)
117
#TODO: (default should be en) same as above
119
return _translate_funcs[lang](abs(nbr), currency)
121
if __name__=='__main__':
126
for i in range(1,200):
127
print i, ">>", int_to_text(i, lang)
128
for i in range(200,999999,139):
129
print i, ">>", int_to_text(i, lang)
131
print int_to_text(int(argv[1]), lang)
134
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: