~serpent-consulting-services/openerp-usa/fix-shipping_api_ups_cc

« back to all changes in this revision

Viewing changes to account_check_writing/amount_to_words.py

  • Committer: npgllc
  • Date: 2012-08-02 17:13:27 UTC
  • Revision ID: npgllc-20120802171327-2xgyyjjb5d1kx26y
Removed all the 6.0 compatible modules

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*-
2
 
##############################################################################
3
 
#
4
 
#    OpenERP, Open Source Management Solution
5
 
#    Copyright (C) 2011 NovaPoint Group LLC (<http://www.novapointgroup.com>)
6
 
#    Copyright (C) 2004-2010 OpenERP SA (<http://www.openerp.com>)
7
 
#
8
 
#    This program is free software: you can redistribute it and/or modify
9
 
#    it under the terms of the GNU General Public License as published by
10
 
#    the Free Software Foundation, either version 3 of the License, or
11
 
#    (at your option) any later version.
12
 
#
13
 
#    This program is distributed in the hope that it will be useful,
14
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 
#    GNU General Public License for more details.
17
 
#
18
 
#    You should have received a copy of the GNU General Public License
19
 
#    along with this program.  If not, see <http://www.gnu.org/licenses/>
20
 
#
21
 
##############################################################################
22
 
 
23
 
# can be used for numbers as large as 999 vigintillion
24
 
# (vigintillion --> 10 to the power 60)
25
 
# tested with Python24      vegaseat      07dec2006
26
 
 
27
 
def int2word(n):
28
 
    """
29
 
    convert an integer number n into a string of English words
30
 
    """
31
 
    # break the number into groups of 3 digits using slicing
32
 
    # each group representing hundred, thousand, million, billion, ...
33
 
    if not n:
34
 
        return 'Zero '
35
 
    n3 = []
36
 
    r1 = ""
37
 
    # create numeric string
38
 
    ns = str(n)
39
 
    for k in range(3, 33, 3):
40
 
        r = ns[-k:]
41
 
        q = len(ns) - k
42
 
        # break if end of ns has been reached
43
 
        if q < -2:
44
 
            break
45
 
        else:
46
 
            if  q >= 0:
47
 
                n3.append(int(r[:3]))
48
 
            elif q >= -1:
49
 
                n3.append(int(r[:2]))
50
 
            elif q >= -2:
51
 
                n3.append(int(r[:1]))
52
 
        r1 = r
53
 
    nw = ""
54
 
    for i, x in enumerate(n3):
55
 
        b1 = x % 10
56
 
        b2 = (x % 100) // 10
57
 
        b3 = (x % 1000) // 100
58
 
        if x == 0:
59
 
            continue  # skip
60
 
        else:
61
 
            t = thousands[i]
62
 
        if b2 == 0:
63
 
            nw = ones[b1] + t + nw
64
 
        elif b2 == 1:
65
 
            nw = tens[b1] + t + nw
66
 
        elif b2 > 1:
67
 
            nw = twenties[b2] + ones[b1] + t + nw
68
 
        if b3 > 0:
69
 
            nw = ones[b3] + "hundred " + nw
70
 
    return nw
71
 
 
72
 
############# globals ################
73
 
 
74
 
ones = ["", "One ", "Two ", "Three ", "Four ", "Five ",
75
 
        "Six ", "Seven ", "Eight ", "Nine "]
76
 
 
77
 
tens = ["Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ",
78
 
        "Fifteen ", "Sixteen ", "Seventeen ", "Eighteen ", "Nineteen "]
79
 
 
80
 
twenties = ["", "", "Twenty ", "Thirty ", "Forty ",
81
 
    "Fifty ", "Sixty ", "Seventy ", "Eighty ", "Ninety "]
82
 
 
83
 
thousands = ["", "Thousand ", "Million ", "Billion ", "Trillion ",
84
 
    "Quadrillion ", "Quintillion ", "Sextillion ", "Septillion ","Octillion ",
85
 
    "Nonillion ", "Decillion ", "Undecillion ", "Duodecillion ", "Tredecillion ",
86
 
    "Quattuordecillion ", "Sexdecillion ", "Septendecillion ", "Octodecillion ",
87
 
    "Novemdecillion ", "Vigintillion "]
88
 
 
89
 
def amount_to_words(num):
90
 
    # select an integer number n for testing or get it from user input
91
 
    res = ""
92
 
    if num < 0:
93
 
        res = "Negative "
94
 
        num = float(str(num)[1:])
95
 
    if num == 0: return 'Zero'
96
 
    else:
97
 
        n = str(num).split('.')
98
 
        return res + int2word(int(n[0])) + 'and ' + (len(n) > 1 and int(n[1]) and str((num - int(num)) * 100).split('.')[0] or 'no') + '/100s'
99
 
    
100
 
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
 
 
b'\\ No newline at end of file'