File Coverage

blib/lib/Lingua/DEU/Num2Word.pm
Criterion Covered Total %
statement 44 50 88.0
branch 15 30 50.0
condition 15 27 55.5
subroutine 8 8 100.0
pod 1 1 100.0
total 83 116 71.5


line stmt bran cond sub pod time code
1             # For Emacs: -*- mode:cperl; eval: (folding-mode 1); coding:utf-8 -*-
2              
3             package Lingua::DEU::Num2Word;
4             # ABSTRACT: Number 2 word conversion in DEU.
5              
6 1     1   190670 use 5.16.0;
  1         5  
7 1     1   7 use utf8;
  1         2  
  1         16  
8 1     1   35 use warnings;
  1         3  
  1         62  
9              
10             # {{{ use block
11              
12 1     1   6 use Carp;
  1         2  
  1         107  
13 1     1   754 use Export::Attrs;
  1         16887  
  1         10  
14 1     1   1045 use Readonly;
  1         5742  
  1         676  
15              
16             # }}}
17             # {{{ variable declarations
18              
19             my Readonly::Scalar $COPY = 'Copyright (c) PetaMem, s.r.o. 2002-present';
20             our $VERSION = '0.2603250';
21              
22             # }}}
23              
24             # {{{ num2deu_cardinal convert number to text
25              
26             sub num2deu_cardinal :Export {
27 7     7 1 251686 my $positive = shift;
28              
29 7 100 66     121 croak 'You should specify a number from interval [0, 999_999_999]'
      66        
      100        
30             if !defined $positive
31             || $positive !~ m{\A\d+\z}xms
32             || $positive < 0
33             || $positive > 999_999_999;
34              
35 5         27 my @tokens1 = qw(null ein zwei drei vier fünf sechs sieben acht neun zehn elf zwölf);
36 5         51 my @tokens2 = qw(zwanzig dreissig vierzig fünfzig sechzig siebzig achtzig neunzig hundert);
37              
38 5 100 66     37 return $tokens1[$positive] if ($positive >= 0 && $positive < 13); # 0 .. 12
39 3 50       10 return 'sechzehn' if ($positive == 16); # 16 exception
40 3 50       8 return 'siebzehn' if ($positive == 17); # 17 exception
41 3 50 33     16 return $tokens1[$positive-10] . 'zehn' if ($positive > 12 && $positive < 20); # 13 .. 19
42              
43 3         11 my $out; # string for return value construction
44             my $one_idx; # index for tokens1 array
45 3         0 my $remain; # remainder
46              
47 3 100 66     27 if ($positive > 19 && $positive < 101) { # 20 .. 100
    100 66        
    50 33        
    0 0        
48 1         4 $one_idx = int ($positive / 10);
49 1         14 $remain = $positive % 10;
50              
51 1 50       20 $out = "$tokens1[$remain]und" if ($remain);
52 1         5 $out .= $tokens2[$one_idx - 2];
53             }
54             elsif ($positive > 100 && $positive < 1000) { # 101 .. 999
55 1         4 $one_idx = int ($positive / 100);
56 1         20 $remain = $positive % 100;
57              
58 1         5 $out = "$tokens1[$one_idx]hundert";
59 1 50       8 $out .= $remain ? num2deu_cardinal($remain) : '';
60             }
61             elsif ($positive > 999 && $positive < 1_000_000) { # 1000 .. 999_999
62 1         5 $one_idx = int ($positive / 1000);
63 1         2 $remain = $positive % 1000;
64              
65 1         4 $out = num2deu_cardinal($one_idx).'tausend';
66 1 50       5 $out .= $remain ? num2deu_cardinal($remain) : '';
67             }
68             elsif ( $positive > 999_999
69             && $positive < 1_000_000_000) { # 1_000_000 .. 999_999_999
70 0         0 $one_idx = int ($positive / 1000000);
71 0         0 $remain = $positive % 1000000;
72 0 0       0 my $one = $one_idx == 1 ? 'e' : '';
73              
74 0         0 $out = num2deu_cardinal($one_idx) . "$one million";
75 0 0       0 $out .= 'en' if ($one_idx > 1);
76 0 0       0 $out .= $remain ? ' ' . num2deu_cardinal($remain) : '';
77             }
78              
79 3         18 return $out;
80 1     1   12 }
  1         2  
  1         11  
81              
82             # }}}
83              
84             1;
85              
86             __END__