File Coverage

blib/lib/Lingua/DEU/Num2Word.pm
Criterion Covered Total %
statement 47 53 88.6
branch 15 30 50.0
condition 15 27 55.5
subroutine 9 9 100.0
pod 1 1 100.0
total 87 120 72.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             # {{{ use block
7              
8 1     1   141431 use 5.10.1;
  1         4  
9              
10 1     1   6 use strict;
  1         2  
  1         48  
11 1     1   5 use warnings;
  1         2  
  1         57  
12 1     1   5 use utf8;
  1         14  
  1         7  
13              
14 1     1   31 use Carp;
  1         2  
  1         84  
15 1     1   926 use Export::Attrs;
  1         13272  
  1         12  
16 1     1   1015 use Readonly;
  1         5276  
  1         578  
17              
18             # }}}
19             # {{{ variable declarations
20              
21             my Readonly::Scalar $COPY = 'Copyright (c) PetaMem, s.r.o. 2002-present';
22             our $VERSION = '0.2603230';
23              
24             # }}}
25              
26             # {{{ num2deu_cardinal convert number to text
27              
28             sub num2deu_cardinal :Export {
29 7     7 1 225135 my $positive = shift;
30              
31 7 100 66     113 croak 'You should specify a number from interval [0, 999_999_999]'
      66        
      100        
32             if !defined $positive
33             || $positive !~ m{\A\d+\z}xms
34             || $positive < 0
35             || $positive > 999_999_999;
36              
37 5         28 my @tokens1 = qw(null ein zwei drei vier fünf sechs sieben acht neun zehn elf zwölf);
38 5         37 my @tokens2 = qw(zwanzig dreissig vierzig fünfzig sechzig siebzig achtzig neunzig hundert);
39              
40 5 100 66     32 return $tokens1[$positive] if ($positive >= 0 && $positive < 13); # 0 .. 12
41 3 50       10 return 'sechzehn' if ($positive == 16); # 16 exception
42 3 50       8 return 'siebzehn' if ($positive == 17); # 17 exception
43 3 50 33     13 return $tokens1[$positive-10] . 'zehn' if ($positive > 12 && $positive < 20); # 13 .. 19
44              
45 3         10 my $out; # string for return value construction
46             my $one_idx; # index for tokens1 array
47 3         0 my $remain; # remainder
48              
49 3 100 66     27 if ($positive > 19 && $positive < 101) { # 20 .. 100
    100 66        
    50 33        
    0 0        
50 1         3 $one_idx = int ($positive / 10);
51 1         6 $remain = $positive % 10;
52              
53 1 50       5 $out = "$tokens1[$remain]und" if ($remain);
54 1         3 $out .= $tokens2[$one_idx - 2];
55             }
56             elsif ($positive > 100 && $positive < 1000) { # 101 .. 999
57 1         5 $one_idx = int ($positive / 100);
58 1         2 $remain = $positive % 100;
59              
60 1         14 $out = "$tokens1[$one_idx]hundert";
61 1 50       8 $out .= $remain ? num2deu_cardinal($remain) : '';
62             }
63             elsif ($positive > 999 && $positive < 1_000_000) { # 1000 .. 999_999
64 1         4 $one_idx = int ($positive / 1000);
65 1         3 $remain = $positive % 1000;
66              
67 1         4 $out = num2deu_cardinal($one_idx).'tausend';
68 1 50       4 $out .= $remain ? num2deu_cardinal($remain) : '';
69             }
70             elsif ( $positive > 999_999
71             && $positive < 1_000_000_000) { # 1_000_000 .. 999_999_999
72 0         0 $one_idx = int ($positive / 1000000);
73 0         0 $remain = $positive % 1000000;
74 0 0       0 my $one = $one_idx == 1 ? 'e' : '';
75              
76 0         0 $out = num2deu_cardinal($one_idx) . "$one million";
77 0 0       0 $out .= 'en' if ($one_idx > 1);
78 0 0       0 $out .= $remain ? ' ' . num2deu_cardinal($remain) : '';
79             }
80              
81 3         16 return $out;
82 1     1   10 }
  1         2  
  1         7  
83              
84             # }}}
85              
86             1;
87              
88             __END__