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
##############################################################################
22
#-------------------------------------------------------------
24
#-------------------------------------------------------------
25
import openobject.logger
26
from openobject.tools.translate import _
28
to_19 = ( 'Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six',
29
'Seven', 'Eight', 'Nine', 'Ten', 'Eleven', 'Twelve', 'Thirteen',
30
'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen' )
31
tens = ( 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety')
33
'Thousand', 'Million', 'Billion', 'Trillion', 'Quadrillion',
34
'Quintillion', 'Sextillion', 'Septillion', 'Octillion', 'Nonillion',
35
'Decillion', 'Undecillion', 'Duodecillion', 'Tredecillion', 'Quattuordecillion',
36
'Sexdecillion', 'Septendecillion', 'Octodecillion', 'Novemdecillion', 'Vigintillion' )
38
# convert a value < 100 to English.
42
for (dcap, dval) in ((k, 20 + (10 * v)) for (v, k) in enumerate(tens)):
45
return dcap + '-' + to_19[val % 10]
48
# convert a value < 1000 to english, special cased because it is the level that kicks
49
# off the < 100 special case. The rest are more general. This also allows you to
50
# get strings in the form of 'forty-five hundred' if called directly.
51
def _convert_nnn(val):
53
(mod, rem) = (val % 100, val // 100)
55
word = to_19[rem] + ' Hundred'
59
word = word + _convert_nn(mod)
62
def english_number(val):
64
return _convert_nn(val)
66
return _convert_nnn(val)
67
for (didx, dval) in ((v - 1, 1000 ** v) for v in range(len(denom))):
72
ret = _convert_nnn(l) + ' ' + denom[didx]
74
ret = ret + ', ' + english_number(r)
77
def amount_to_text(number, currency):
79
list = str(number).split('.')
80
start_word = english_number(int(list[0]))
81
end_word = english_number(int(list[1]))
82
cents_number = int(list[1])
83
cents_name = (cents_number > 1) and 'Cents' or 'Cent'
84
final_result = start_word +' '+units_name+' and ' + end_word +' '+cents_name
88
#-------------------------------------------------------------
90
#-------------------------------------------------------------
92
_translate_funcs = {'en' : amount_to_text}
94
#TODO: we should use the country AND language (ex: septante VS soixante dix)
95
#TODO: we should use en by default, but the translation func is yet to be implemented
96
def amount_to_text(nbr, lang='en', currency='euro'):
98
Converts an integer to its textual representation, using the language set in the context if any.
100
1654: thousands six cent cinquante-quatre.
102
import openobject.netsvc
104
if not _translate_funcs.has_key(lang):
105
openobject.logger.Logger().notifyChannel('translate', openobject.logger.LOG_WARNING, _("no translation function found for lang: '%s'" % (lang,)))
106
#TODO: (default should be en) same as above
108
return _translate_funcs[lang](abs(nbr), currency)
110
if __name__=='__main__':
115
for i in range(1,200):
116
print i, ">>", int_to_text(i, lang)
117
for i in range(200,999999,139):
118
print i, ">>", int_to_text(i, lang)
120
print int_to_text(int(argv[1]), lang)