blib/lib/Lingua/RON/Num2Word.pm untested

filter: Statementstmt 0.0% 0 / 57 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 36 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 141 CC 21 SCAR 58.9
CC
21
Cov
0%
num2ron_cardinal
462.0
BEGIN
2.0
BEGIN
2.0
CRAP 359.8
1 # For Emacs: -*- mode:cperl; eval: (folding-mode 1); coding:utf-8 -*-
2  
3 package Lingua::RON::Num2Word;
4 # ABSTRACT: Number to word conversion in Romanian
5  
6 use 5.16.0;
7 use utf8;
8 use warnings;
9  
10 # {{{ use block
11  
12 use Carp;
13 use Export::Attrs;
14 use Readonly;
15  
16 # }}}
17 # {{{ variable declarations
18  
19 my Readonly::Scalar $COPY = 'Copyright (c) PetaMem, s.r.o. 2002-present';
20 our $VERSION = '0.2603260';
21  
22 # }}}
23  
24 # {{{ num2ron_cardinal convert number to text
25  
26 sub num2ron_cardinal :Export {
27 my $positive = shift;
28  
29 croak 'You should specify a number from interval [0, 999_999_999]'
30 if !defined $positive
31 || $positive !~ m{\A\d+\z}xms
32 || $positive < 0
33 || $positive > 999_999_999;
34  
35 my @units = ('zero', 'unu', 'doi', 'trei', 'patru', 'cinci',
36 'șase', 'șapte', 'opt', 'nouă');
37  
38 my @teens = ('zece', 'unsprezece', 'doisprezece', 'treisprezece',
39 'paisprezece', 'cincisprezece', 'șaisprezece',
40 'șaptesprezece', 'optsprezece', 'nouăsprezece');
41  
42 my @tens = ('', '', 'douăzeci', 'treizeci', 'patruzeci', 'cincizeci',
43 'șaizeci', 'șaptezeci', 'optzeci', 'nouăzeci');
44  
45 return $units[$positive] if ($positive >= 0 && $positive < 10);
46 return $teens[$positive - 10] if ($positive >= 10 && $positive < 20);
47  
48 my $out;
49 my $remain;
50  
51 if ($positive > 19 && $positive < 100) { # 20 .. 99
52 my $ten_idx = int($positive / 10);
53 $remain = $positive % 10;
54  
55 $out = $tens[$ten_idx];
56 $out .= ' și ' . $units[$remain] if ($remain);
57 }
58 elsif ($positive == 100) { # 100
59 $out = 'o sută';
60 }
61 elsif ($positive > 100 && $positive < 200) { # 101 .. 199
62 $remain = $positive % 100;
63 $out = 'o sută ' . num2ron_cardinal($remain);
64 }
65 elsif ($positive >= 200 && $positive < 1000) { # 200 .. 999
66 my $hun_idx = int($positive / 100);
67 $remain = $positive % 100;
68  
69 $out = $hun_idx == 2
70 ? 'două sute'
71 : num2ron_cardinal($hun_idx) . ' sute';
72 $out .= ' ' . num2ron_cardinal($remain) if ($remain);
73 }
74 elsif ($positive >= 1000 && $positive < 2000) { # 1000 .. 1999
75 $remain = $positive % 1000;
76  
77 $out = 'o mie';
78 $out .= ' ' . num2ron_cardinal($remain) if ($remain);
79 }
80 elsif ($positive >= 2000 && $positive < 1_000_000) { # 2000 .. 999_999
81 my $tho_idx = int($positive / 1000);
82 $remain = $positive % 1000;
83  
84 $out = $tho_idx == 2
85 ? 'două mii'
86 : num2ron_cardinal($tho_idx) . ' mii';
87 $out .= ' ' . num2ron_cardinal($remain) if ($remain);
88 }
89 elsif ($positive >= 1_000_000 && $positive < 2_000_000) { # 1_000_000 .. 1_999_999
90 $remain = $positive % 1_000_000;
91  
92 $out = 'un milion';
93 $out .= ' ' . num2ron_cardinal($remain) if ($remain);
94 }
95 elsif ($positive >= 2_000_000 && $positive < 1_000_000_000) { # 2_000_000 .. 999_999_999
96 my $mil_idx = int($positive / 1_000_000);
97 $remain = $positive % 1_000_000;
98  
99 $out = $mil_idx == 2
100 ? 'două milioane'
101 : num2ron_cardinal($mil_idx) . ' milioane';
102 $out .= ' ' . num2ron_cardinal($remain) if ($remain);
103 }
104  
105 return $out;
106 }
107  
108 # }}}
109  
110 1;
111  
112 __END__