File Coverage

blib/lib/Lingua/EN/Number/Format/MixWithWords.pm
Criterion Covered Total %
statement 29 29 100.0
branch 4 6 66.6
condition 3 7 42.8
subroutine 8 8 100.0
pod 1 2 50.0
total 45 52 86.5


line stmt bran cond sub pod time code
1             package Lingua::EN::Number::Format::MixWithWords;
2              
3 2     2   810 use 5.010001;
  2         6  
  2         76  
4 2     2   10 use strict;
  2         4  
  2         106  
5 2     2   12 use warnings;
  2         3  
  2         49  
6              
7 2     2   1318 use Lingua::Base::Number::Format::MixWithWords;
  2         6  
  2         78  
8 2     2   1763 use parent qw(Lingua::Base::Number::Format::MixWithWords);
  2         633  
  2         11  
9              
10 2     2   1902 use Exporter::Lite;
  2         1577  
  2         12  
11              
12             our @EXPORT_OK = qw(format_number_mix);
13              
14             our $VERSION = '0.07'; # VERSION
15              
16             our %SPEC;
17              
18             $SPEC{format_number_mix} = {
19             summary => '',
20             args => {
21             num => ['float*' => {
22             summary => 'The input number to format',
23             }],
24             scale => ['str*' => {
25             summary => 'Pick long or short scale names',
26             description => <<_,
27             See http://en.wikipedia.org/wiki/Long_scale#Long_scale_countries_and_languages
28             for details.
29             _
30             in => ['short', 'long'],
31             }],
32             num_decimal => ['int' => {
33             summary => 'Number of decimal points to round',
34             description => <<'_',
35             Can be negative, e.g. -1 to round to nearest 10, -2 to nearest 100, and so on.
36             _
37             }],
38             min_format => ['float*' => {
39             summary => 'Number must be larger than this to be formatted as '.
40             'mixture of number and word',
41             default => 1000000,
42             }],
43             min_fraction => ['float*' => {
44             summary => 'Whether smaller number can be formatted with 0,x',
45             description => <<_,
46             If min_fraction is 1 (the default) or 0.9, 800000 won't be formatted as 0.9
47             omillion but will be if min_fraction is 0.8.
48             _
49             default => 1,
50             min => 0,
51             max => 1,
52             }],
53             },
54             result_naked => 1,
55             };
56             sub format_number_mix {
57 18     18 1 39218 my %args = @_;
58              
59 18         137 my $f = __PACKAGE__->new(
60             num_decimal => $args{num_decimal},
61             min_format => $args{min_format},
62             min_fraction => $args{min_fraction},
63             scale => $args{scale},
64             );
65 18         103 $f->_format($args{num});
66             }
67              
68             my $en_short_names = {
69             #2 => 'hundred',
70             3 => 'thousand',
71             6 => 'million',
72             9 => 'billion',
73             12 => 'trillion',
74             15 => 'quadrillion',
75             18 => 'quintillion',
76             21 => 'sextillion',
77             24 => 'septillion',
78             27 => 'octillion',
79             30 => 'nonillion',
80             33 => 'decillion',
81             36 => 'undecillion',
82             39 => 'duodecillion',
83             42 => 'tredecillion',
84             45 => 'quattuordecillion',
85             48 => 'quindecillion',
86             51 => 'sexdecillion',
87             54 => 'septendecillion',
88             57 => 'octodecillion',
89             60 => 'novemdecillion',
90             63 => 'vigintillion',
91             100 => 'googol',
92             303 => 'centillion',
93             };
94              
95             my $en_long_names = {
96             #2 => 'hundred',
97             3 => 'thousand',
98             6 => 'million',
99             12 => 'billion',
100             15 => 'billiard',
101             18 => 'trillion',
102             24 => 'quadrillion',
103             30 => 'quintillion',
104             36 => 'sextillion',
105             42 => 'septillion',
106             48 => 'octillion',
107             54 => 'nonillion',
108             60 => 'decillion',
109             66 => 'undecillion',
110             72 => 'duodecillion',
111             78 => 'tredecillion',
112             84 => 'quattuordecillion',
113             90 => 'quindecillion',
114             96 => 'sexdecillion',
115             102 => 'septendecillion',
116             108 => 'octodecillion',
117             114 => 'novemdecillion',
118             120 => 'vigintillion',
119             100 => 'googol',
120             600 => 'centillion',
121             };
122              
123             sub new {
124 18     18 0 93 my ($class, %args) = @_;
125 18   50     119 $args{decimal_point} //= ".";
126 18   50     81 $args{thousands_sep} //= ",";
127 18 50       52 die "Please specify scale" unless $args{scale};
128 18 50       105 die "Invalid scale, please use short/long"
129             unless $args{scale} =~ /\A(short|long)\z/;
130 18 100 33     111 $args{names} //= ($args{scale} eq 'long' ? $en_long_names:$en_short_names);
131             # XXX should use "SUPER"
132 18         109 my $self = Lingua::Base::Number::Format::MixWithWords->new(%args);
133 18         95 bless $self, $class;
134             }
135              
136             1;
137             # ABSTRACT: Format number to a mixture of numbers and words (e.g. 12.3 million)
138              
139             __END__