line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Lingua::NO::Num2Word; |
2
|
|
|
|
|
|
|
$Lingua::NO::Num2Word::VERSION = '0.011'; |
3
|
|
|
|
|
|
|
|
4
|
1
|
|
|
1
|
|
940
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
760
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
=head1 NAME |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
Lingua::NO::Num2Word - convert whole number to norwegian text. Output text is in ISO-8859-1 encoding. |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 SYNOPSIS |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
use Lingua::NO::Num2Word; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
my $no_num2word = Lingua::NO::Num2Word->new(); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
my $text = $no_num2word->num2no_cardinal( 1000000 ); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
print $text || "Sorry, can't convert this number into norwegian."; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 DESCRIPTION |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
This module is based on and inspired by Roman Vasicek module Lingua::CS::Num2Word. Lingua::NO::Num2Word is a module for converting whole numbers into their norwegian textual representation. Converts numbers from 0 up to 999 999 999. |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=head1 METHODS |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
The following methods are provided by the Lingua::NO::Num2Word class. |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=over 2 |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=cut |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
my %group1 = qw( 0 null 1 en 2 to |
33
|
|
|
|
|
|
|
3 tre 4 fire 5 fem |
34
|
|
|
|
|
|
|
6 seks 7 sju 8 åtte |
35
|
|
|
|
|
|
|
9 ni 10 ti 11 ellve |
36
|
|
|
|
|
|
|
12 tolv 13 tretten 14 fjorten |
37
|
|
|
|
|
|
|
15 femten 16 seksten 17 sytten |
38
|
|
|
|
|
|
|
18 atten 19 nitten |
39
|
|
|
|
|
|
|
); |
40
|
|
|
|
|
|
|
my %group2 = qw( 20 tjue 30 tretti 40 førti |
41
|
|
|
|
|
|
|
50 femti 60 seksti 70 sytti |
42
|
|
|
|
|
|
|
80 åtti 90 nitti |
43
|
|
|
|
|
|
|
); |
44
|
|
|
|
|
|
|
my %group3 = ( 100, 'ett hundre', 200, 'to hundre', 300, 'tre hundre', |
45
|
|
|
|
|
|
|
400, 'fire hundre', 500, 'fem hundre', 600, 'seks hundre', |
46
|
|
|
|
|
|
|
700, 'sju hundre', 800, 'åtte hundre', 900, 'ni hundre' |
47
|
|
|
|
|
|
|
); |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
my $singleton; |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=pod |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=item B |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
Create a singleton object. |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
my $no_num2word = Lingua::NO::Num2Word->new(); |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=cut |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub new |
62
|
|
|
|
|
|
|
{ |
63
|
2
|
|
|
2
|
1
|
1376
|
my $invocant = shift; |
64
|
|
|
|
|
|
|
|
65
|
2
|
|
33
|
|
|
14
|
my $class = ref ( $invocant ) || $invocant; |
66
|
2
|
|
100
|
|
|
14
|
$singleton ||= bless {}, $class; |
67
|
|
|
|
|
|
|
|
68
|
2
|
|
|
|
|
5
|
return $singleton; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=pod |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=item B |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
Converts a whole number to norwegian language. |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
my $text = $no_num2word->num2no_cardinal( 1000000 ); |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=cut |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub num2no_cardinal |
82
|
|
|
|
|
|
|
{ |
83
|
53
|
|
|
53
|
1
|
339
|
my $self = shift; |
84
|
|
|
|
|
|
|
|
85
|
53
|
|
|
|
|
61
|
my $result = ''; |
86
|
53
|
100
|
|
|
|
103
|
my $number = defined $_[0] ? shift : return $result; |
87
|
|
|
|
|
|
|
#print "$number -> "; |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
# check if number has decimals > 0, allowing whole numbers written as 2.00 |
90
|
52
|
|
|
|
|
92
|
$number =~ /(\d+)\.(\d+)/; |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
# numbers less than 0 are not supported and numbers containg decimals greater than 0 |
93
|
52
|
100
|
66
|
|
|
237
|
return $result if ( $number < 0 || ( defined $2 && $2 > 0 ) ); |
|
|
|
66
|
|
|
|
|
94
|
|
|
|
|
|
|
|
95
|
50
|
|
|
|
|
57
|
my $reminder = 0; |
96
|
|
|
|
|
|
|
|
97
|
50
|
100
|
|
|
|
126
|
if ( $number < 20 ) |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
98
|
|
|
|
|
|
|
{ |
99
|
19
|
|
|
|
|
30
|
$result = $group1{$number}; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
elsif ( $number < 100 ) |
103
|
|
|
|
|
|
|
{ |
104
|
9
|
|
|
|
|
11
|
$reminder = $number % 10; |
105
|
9
|
100
|
|
|
|
16
|
if ( $reminder == 0 ) |
106
|
|
|
|
|
|
|
{ |
107
|
1
|
|
|
|
|
3
|
$result = $group2{$number}; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
else |
110
|
|
|
|
|
|
|
{ |
111
|
8
|
|
|
|
|
41
|
$result = $group2{$number - $reminder} . ' ' . $self->num2no_cardinal( $reminder ); |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
elsif ($number < 1000) |
116
|
|
|
|
|
|
|
{ |
117
|
9
|
|
|
|
|
11
|
$reminder = $number % 100; |
118
|
9
|
100
|
|
|
|
15
|
if ( $reminder == 0 ) |
119
|
|
|
|
|
|
|
{ |
120
|
2
|
|
|
|
|
4
|
$result = $group3{$number}; |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
else |
123
|
|
|
|
|
|
|
{ |
124
|
7
|
|
|
|
|
26
|
$result = $group3{$number - $reminder} . ' og ' . $self->num2no_cardinal( $reminder ); |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
elsif ( $number < 1000000 ) |
129
|
|
|
|
|
|
|
{ |
130
|
8
|
|
|
|
|
10
|
$reminder = $number % 1000; |
131
|
|
|
|
|
|
|
|
132
|
8
|
100
|
|
|
|
25
|
my $tmp1 = ( $reminder != 0 ) ? ' '.$self->num2no_cardinal($reminder) : ''; |
133
|
8
|
|
|
|
|
21
|
my $tmp2 = substr( $number, 0, length( $number ) - 3 ); |
134
|
8
|
|
|
|
|
15
|
my $tmp3 = $tmp2 % 10; |
135
|
|
|
|
|
|
|
|
136
|
8
|
|
|
|
|
10
|
my $space = ''; |
137
|
8
|
100
|
100
|
|
|
32
|
$space = ' og' if ( $reminder < 100 && $reminder != 0 ); |
138
|
|
|
|
|
|
|
|
139
|
8
|
100
|
66
|
|
|
27
|
if ( $tmp3 == 1 && $tmp2 == 1 ) |
140
|
|
|
|
|
|
|
{ |
141
|
1
|
|
|
|
|
3
|
$tmp2 = 'ett tusen'; |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
else |
144
|
|
|
|
|
|
|
{ |
145
|
7
|
|
|
|
|
18
|
$tmp2 = $self->num2no_cardinal($tmp2) . ' tusen'; |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
8
|
|
|
|
|
20
|
$result = $tmp2 . $space . $tmp1; |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
elsif ( $number < 1000000000 ) |
152
|
|
|
|
|
|
|
{ |
153
|
4
|
|
|
|
|
6
|
$reminder = $number % 1000000; |
154
|
|
|
|
|
|
|
|
155
|
4
|
100
|
|
|
|
11
|
my $tmp1 = ( $reminder != 0 ) ? ' ' . $self->num2no_cardinal($reminder) : ''; |
156
|
4
|
|
|
|
|
7
|
my $tmp2 = substr( $number, 0, length( $number ) - 6 ); |
157
|
4
|
|
|
|
|
8
|
my $tmp3 = $tmp2 % 10; |
158
|
|
|
|
|
|
|
|
159
|
4
|
|
|
|
|
4
|
my $space = ''; |
160
|
4
|
100
|
100
|
|
|
19
|
$space = ' og' if ( $reminder < 100000 && $reminder != 0 ); |
161
|
|
|
|
|
|
|
|
162
|
4
|
100
|
66
|
|
|
16
|
if ( $tmp3 == 1 && $tmp2 == 1 ) |
163
|
|
|
|
|
|
|
{ |
164
|
1
|
|
|
|
|
2
|
$tmp2 = 'en million'; |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
else |
167
|
|
|
|
|
|
|
{ |
168
|
3
|
|
|
|
|
6
|
$tmp2 = $self->num2no_cardinal( $tmp2 ) . ' millioner'; |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
|
171
|
4
|
|
|
|
|
23
|
$result = $tmp2 . $space . $tmp1; |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
} |
174
|
|
|
|
|
|
|
else |
175
|
|
|
|
|
|
|
{ |
176
|
|
|
|
|
|
|
# >= 1 000 000 000 unsupported |
177
|
|
|
|
|
|
|
} |
178
|
|
|
|
|
|
|
|
179
|
50
|
|
|
|
|
156
|
return $result; |
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
1; |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=back |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=head1 HISTORY |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
* [16.06.2004] Version 0.011 released. |
189
|
|
|
|
|
|
|
* [13.06.2004] Version 0.01 released. |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=head1 VERSION |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
This is version 0.011 |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
=head1 AUTHOR |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
Kjetil Fikkan (kjetil@fikkan.org) |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=head1 COPYRIGHT |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
Copyright (c) 2004 Kjetil Fikkan |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
This module is free software. It may be used, redistributed |
204
|
|
|
|
|
|
|
and/or modified under the same terms as Perl itself. |