This commit was generated by cvs2svn to compensate for changes in r3880,
[freeside.git] / sql-ledger / old / sql-ledger / locale / it / Num2text
1 #=====================================================================
2 # SQL-Ledger Accounting
3 # Copyright (C) 2002
4 #
5 #  Author: Dieter Simader
6 #   Email: dsimader@sql-ledger.org
7 #     Web: http://www.sql-ledger.org
8 #
9 #  Contributors: Luca Venturini <luca@yepa.com>
10 #
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
15 #
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #======================================================================
24 #
25 # this is a variation of the Lingua package
26 # written for check and receipt printing
27 # it returns a properly formatted text string
28 # for a number up to 10**12
29
30 sub init {
31   my $self = shift;
32
33   %{ $self->{numbername} } =
34                    (0 => 'Zero',
35                     1 => 'uno',
36                     2 => 'due',
37                     3 => 'tre',
38                     4 => 'quattro',
39                     5 => 'cinque',
40                     6 => 'sei',
41                     7 => 'sette',
42                     8 => 'otto',
43                     9 => 'nove',
44                    10 => 'dieci',
45                    11 => 'undici',
46                    12 => 'dodici',
47                    13 => 'tredici',
48                    14 => 'quattrodici',
49                    15 => 'quindici',
50                    16 => 'sedici',
51                    17 => 'diciassette',
52                    18 => 'diciotto',
53                    19 => 'diciannove',
54                    20 => 'venti',
55                    30 => 'trenta',
56                    40 => 'quaranta',
57                    50 => 'cinquanta',
58                    60 => 'sessanta',
59                    70 => 'settanta',
60                    80 => 'ottanta',
61                    90 => 'novanta',
62                 10**2 => 'cento',
63                 10**3 => 'mille',
64                 10**6 => 'milione',
65                 10**9 => 'miliardo',
66                10**12 => 'mille miliardi'
67                 );
68
69 }
70
71
72 sub num2text {
73   my ($self, $amount) = @_;
74
75   return $self->{numbername}{0} unless $amount;
76
77   my @textnumber = ();
78
79   # split amount into chunks of 3
80   my @num = reverse split //, $amount;
81   my @numblock = ();
82   my ($i, $appendn);
83   my @a = ();
84
85   while (@num) {
86     @a = ();
87     for (1 .. 3) {
88       push @a, shift @num;
89     }
90     push @numblock, join / /, reverse @a;
91   }
92   
93   while (@numblock) {
94
95     $i = $#numblock;
96     @num = split //, $numblock[$i];
97     
98     $numblock[$i] *= 1;
99     
100     if ($numblock[$i] == 0) {
101       pop @numblock;
102       next;
103     }
104    
105     if ($numblock[$i] > 99) {
106       # the one from hundreds
107       push @textnumber, $self->{numbername}{$num[0]};
108      
109       # add hundred designation
110       push @textnumber, $self->{numbername}{10**2};
111
112       # reduce numblock
113       $numblock[$i] -= $num[0] * 100;
114     }
115     
116     if ($numblock[$i] > 9) {
117       # tens
118       push @textnumber, $self->format_ten($numblock[$i]);
119     } elsif ($numblock[$i] > 1) {
120       # ones
121       push @textnumber, $self->{numbername}{$numblock[$i]};
122     }
123     
124     # add thousand, million
125     if ($i) {
126       $amount = 10**($i * 3);
127       push @textnumber, $self->{numbername}{$amount};
128     }
129     
130     pop @numblock;
131     
132   }
133
134   join '', @textnumber;
135
136 }
137
138
139 sub format_ten {
140   my ($self, $amount) = @_;
141   
142   my $textnumber = "";
143   my @num = split //, $amount;
144
145   if ($amount > 20) {
146     if ($num[1] == 0) { 
147       $textnumber = $self->{numbername}{$amount}; 
148     } else {
149         $amount = $num[0] * 10;
150         $textnumber = $self->{numbername}{$amount}.$self->{numbername}{$num[1]};
151       }
152     }
153   } else {
154     $textnumber = $self->{numbername}{$amount};
155   }
156   
157   $textnumber;
158   
159 }
160
161
162 1;
163