File: Num2text.pm

package info (click to toggle)
sql-ledger 2.8.30-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 34,304 kB
  • ctags: 3,640
  • sloc: perl: 55,210; sql: 27,183; makefile: 129; sh: 38
file content (147 lines) | stat: -rw-r--r-- 2,837 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#=====================================================================
# SQL-Ledger ERP
# Copyright (C) 2006
#
#  Author: DWS Systems Inc.
#     Web: http://www.sql-ledger.com
#
#=====================================================================
#
# this is the default code for the Check package
#
#=====================================================================


sub init {
  my $self = shift;

  %{ $self->{numbername} } =
                   (0 => 'Zero',
                    1 => 'One',
                    2 => 'Two',
	            3 => 'Three',
		    4 => 'Four',
		    5 => 'Five',
		    6 => 'Six',
		    7 => 'Seven',
		    8 => 'Eight',
		    9 => 'Nine',
		   10 => 'Ten',
		   11 => 'Eleven',
		   12 => 'Twelve',
		   13 => 'Thirteen',
		   14 => 'Fourteen',
		   15 => 'Fifteen',
		   16 => 'Sixteen',
		   17 => 'Seventeen',
		   18 => 'Eighteen',
		   19 => 'Nineteen',
		   20 => 'Twenty',
		   30 => 'Thirty',
		   40 => 'Forty',
		   50 => 'Fifty',
		   60 => 'Sixty',
		   70 => 'Seventy',
		   80 => 'Eighty',
		   90 => 'Ninety',
                10**2 => 'Hundred',
                10**3 => 'Thousand',
		10**6 => 'Million',
		10**9 => 'Billion',
	       10**12 => 'Trillion',
		);

}


sub num2text {
  my ($self, $amount) = @_;

  return $self->{numbername}{0} unless $amount;

  my @textnumber = ();

  # split amount into chunks of 3
  my @num = reverse split //, abs($amount);
  my @numblock = ();
  my @a;
  my $i;

  while (@num) {
    @a = ();
    for (1 .. 3) {
      push @a, shift @num;
    }
    push @numblock, join / /, reverse @a;
  }
    
  while (@numblock) {

    $i = $#numblock;
    @num = split //, $numblock[$i];
    
    if ($numblock[$i] == 0) {
      pop @numblock;
      next;
    }
   
    if ($numblock[$i] > 99) {
      # the one from hundreds
      push @textnumber, $self->{numbername}{$num[0]};
     
      # add hundred designation
      push @textnumber, $self->{numbername}{10**2};

      # reduce numblock
      $numblock[$i] -= $num[0] * 100;
      
    }
    
    $numblock[$i] *= 1;
    
    if ($numblock[$i] > 9) {
      # tens
      push @textnumber, $self->format_ten($numblock[$i]);
    } elsif ($numblock[$i] > 0) {
      # ones
      push @textnumber, $self->{numbername}{$numblock[$i]};
    }
    
    # add thousand, million
    if ($i) {
      $num = 10**($i * 3);
      push @textnumber, $self->{numbername}{$num};
    }
      
    pop @numblock;
    
  }

  join ' ', @textnumber;

}


sub format_ten {
  my ($self, $amount) = @_;
  
  my $textnumber = "";
  my @num = split //, $amount;

  if ($amount > 20) {
    $textnumber = $self->{numbername}{$num[0]*10};
    $amount = $num[1];
  } else {
    $textnumber = $self->{numbername}{$amount};
    $amount = 0;
  }

  $textnumber .= " ".$self->{numbername}{$amount} if $amount;

  $textnumber;
  
}


1;