File Coverage

blib/lib/Lingua/FAS/Num2Word.pm
Criterion Covered Total %
statement 27 49 55.1
branch 2 26 7.6
condition 3 9 33.3
subroutine 8 9 88.8
pod 2 2 100.0
total 42 95 44.2


line stmt bran cond sub pod time code
1             # For Emacs: -*- mode:cperl; eval: (folding-mode 1); coding:utf-8 -*-
2              
3             package Lingua::FAS::Num2Word;
4             # ABSTRACT: Number to word conversion in Persian
5              
6 1     1   90756 use 5.16.0;
  1         3  
7 1     1   4 use utf8;
  1         1  
  1         10  
8 1     1   19 use warnings;
  1         3  
  1         46  
9              
10             # {{{ use block
11              
12 1     1   4 use Carp;
  1         2  
  1         84  
13 1     1   549 use Export::Attrs;
  1         8799  
  1         7  
14 1     1   683 use Readonly;
  1         3311  
  1         452  
15              
16             # }}}
17             # {{{ variable declarations
18              
19             my Readonly::Scalar $COPY = 'Copyright (c) PetaMem, s.r.o. 2002-present';
20             our $VERSION = '0.2603300';
21              
22             # }}}
23              
24             # {{{ num2fas_cardinal convert number to text
25              
26             sub num2fas_cardinal :Export {
27 2     2 1 172301 my $positive = shift;
28              
29 2 50 33     37 croak 'You should specify a number from interval [0, 999_999_999]'
      33        
      33        
30             if !defined $positive
31             || $positive !~ m{\A\d+\z}xms
32             || $positive < 0
33             || $positive > 999_999_999;
34              
35 2         14 my @ones = ('صفر', 'یک', 'دو', 'سه', 'چهار', 'پنج', 'شش', 'هفت', 'هشت', 'نه');
36 2         10 my @teens = ('ده', 'یازده', 'دوازده', 'سیزده', 'چهارده', 'پانزده',
37             'شانزده', 'هفده', 'هجده', 'نوزده');
38 2         10 my @tens = ('بیست', 'سی', 'چهل', 'پنجاه', 'شصت', 'هفتاد', 'هشتاد', 'نود');
39 2         8 my @hundreds = ('صد', 'دویست', 'سیصد', 'چهارصد', 'پانصد',
40             'ششصد', 'هفتصد', 'هشتصد', 'نهصد');
41              
42             # 0 .. 9
43 2 50       18 return $ones[$positive] if $positive < 10;
44              
45             # 10 .. 19
46 0 0         return $teens[$positive - 10] if $positive < 20;
47              
48             # 20 .. 99
49 0 0         if ($positive < 100) {
50 0           my $ten_idx = int($positive / 10) - 2;
51 0           my $remain = $positive % 10;
52              
53 0 0         return $tens[$ten_idx] if $remain == 0;
54 0           return $tens[$ten_idx] . ' و ' . $ones[$remain];
55             }
56              
57 0           my $out;
58             my $remain;
59              
60             # 100 .. 999
61 0 0         if ($positive < 1000) {
    0          
    0          
62 0           my $hun_idx = int($positive / 100) - 1;
63 0           $remain = $positive % 100;
64              
65 0           $out = $hundreds[$hun_idx];
66 0 0         $out .= $remain ? ' و ' . num2fas_cardinal($remain) : '';
67             }
68             # 1000 .. 999_999
69             elsif ($positive < 1_000_000) {
70 0           my $tho_idx = int($positive / 1000);
71 0           $remain = $positive % 1000;
72              
73 0 0         $out = $tho_idx == 1 ? 'هزار' : num2fas_cardinal($tho_idx) . ' هزار';
74 0 0         $out .= $remain ? ' و ' . num2fas_cardinal($remain) : '';
75             }
76             # 1_000_000 .. 999_999_999
77             elsif ($positive < 1_000_000_000) {
78 0           my $mil_idx = int($positive / 1_000_000);
79 0           $remain = $positive % 1_000_000;
80              
81 0 0         $out = $mil_idx == 1 ? 'یک میلیون' : num2fas_cardinal($mil_idx) . ' میلیون';
82 0 0         $out .= $remain ? ' و ' . num2fas_cardinal($remain) : '';
83             }
84              
85 0           return $out;
86 1     1   11 }
  1         2  
  1         9  
87              
88             # }}}
89              
90              
91             # {{{ capabilities declare supported features
92              
93             sub capabilities {
94             return {
95 0     0 1   cardinal => 1,
96             ordinal => 0,
97             };
98             }
99              
100             # }}}
101             1;
102              
103             __END__