CLI/Util/CurrencyUtil.pm

filter: Statementstmt 75.9% 41 / 54 Branchbran 40.9% 9 / 22 Conditioncond 58.8% 10 / 17 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 66.6% 8 / 12 Podpod 100% 7 / 7 totaltotal 63.3% 76 / 120 CC 19 SCAR 24.7
CC
19
Cov
65%
format_fiat_currency
22.7
format_currency
6.8
_log10
2.3
CRAP 11.8
« CLI/Util/DateUtil.pm ↑ Summary CLI/Converter.pm »
1 package Finance::CoinbasePro::API::CLI::Util::CurrencyUtil;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 our @EXPORT_OK = qw( get_product_currencies
6 format_crypto format_crypto_exact
7 format_crypto_with_usd format_usd format_currency );
8  
9 1 ▶use POSIX qw( ceil floor );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 # our global number formatter.
11 1 ▶use Number::Format;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 my $formatter = Number::Format->new( -neg_format=>'-x' );
13  
14 sub get_product_currencies {
15 0 ▶ my $product = shift;
Subroutine get_product_currencies: not called CC=1 SCAR=6.9
Pod documented
16 0 my ($from_currency, $to_currency) = split(/-/, $product);
17 0 return ($from_currency, $to_currency);
18 }
19  
20 sub format_currency {
21 my ($val, $currency, $prec) = @_;
22 $currency ||= "USD";
23 if ($currency eq "USD" or $currency eq "EUR" or $currency eq "GBP" or $currency eq "JPY") {
24 return format_fiat_currency($val, $currency, $prec);
25 }
26 return format_crypto( $val, $currency );
27 }
28 sub format_currency {
29 7 ▶ my ($val, $currency, $prec) = @_;
Subroutine format_currency: called CC=6 SCAR=19.1
Pod documented
30 7 ▶ $currency ||= "USD";
Condition50%1/2
$currency ||= "USD"
l!l
70
MC/DC0%0/1
$currency ||= "USD"
$currency
Truth table50%1/2
A
A= $currency
Aresult
11
00
31 7 ▶ if ($currency eq "USD" or $currency eq "EUR" or $currency eq "GBP" or $currency eq "JPY") {
Branch: 1 branch
BranchTrueFalseTotal
if ($currency eq "USD" or $currency eq "EUR" or $currency eq "GBP" or $currency eq "JPY") 0 4 4
Condition66%6/9
$currency eq "USD" or $currency eq "EUR" or $currency eq "GBP" or $currency eq "JPY"
l!l&&r!l&&!r
304
$currency eq "USD" or $currency eq "EUR" or $currency eq "GBP"
l!l&&r!l&&!r
304
$currency eq "USD" or $currency eq "EUR"
l!l&&r!l&&!r
304
MC/DC25%1/4
$currency eq "USD" or $currency eq "EUR" or $currency eq "GBP" or $currency eq "JPY"
$currency eq "USD" $currency eq "EUR" $currency eq "GBP" $currency eq "JPY"
Truth table40%2/5
A or B or C or D
A= $currency eq "USD"B= $currency eq "EUR"C= $currency eq "GBP"D= $currency eq "JPY"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
32 3 return format_fiat_currency($val, $currency, $prec);
33 }
34 4 return format_crypto( $val, $currency );
35 }
36 sub format_crypto {
37 4 ▶ my ($val, $currency) = @_;
Subroutine format_crypto: called CC=1 SCAR=0.0
Pod documented
38 4 sprintf("%0.4f%s", $val, $currency ); # like 0.234BTC
39 }
40 sub format_crypto_exact {
41 0 ▶ my ($val, $currency) = @_;
Subroutine format_crypto_exact: not called CC=1 SCAR=6.9
Pod documented
42 0 sprintf("%0.10f%s", $val, $currency ); # like 0.0123456789BTC
43 }
44 sub format_crypto_with_usd {
45 0 ▶ my ($val, $currency, $per_usd) = @_;
Subroutine format_crypto_with_usd: not called CC=1 SCAR=6.9
Pod documented
46 0 sprintf( "%s (%s)", format_crypto( $val, $currency), format_usd( $val * $per_usd ) );
47 }
48  
49 sub format_usd {
50 0 ▶ my ($val, $prec) = @_;
Subroutine format_usd: not called CC=1 SCAR=6.9
Pod documented
51 0 return format_fiat_currency( $val, "USD", $prec );
52 }
53  
54 sub format_fiat_currency {
55 3 ▶ my ($val, $currency, $prec) = @_;
Subroutine format_fiat_currency: called CC=13 SCAR=31.2
Pod documented
56 3 ▶ my $indicator = $currency eq "USD" ? '$' :
Branch: 4 branches
BranchTrueFalseTotal
$currency eq 'USD' ? : 3 0 3
$currency eq 'EUR' ? : 0 0 0
$currency eq 'GBP' ? : 0 0 0
$currency eq 'JPY' ? : 0 0 0
57 $currency eq "EUR" ? '€' :
58 $currency eq "GBP" ? '£' :
59 $currency eq "JPY" ? '¥' :
60 $currency;
61  
62 3 ▶ $val ||= 0;
Condition50%1/2
$val ||= 0
l!l
30
MC/DC0%0/1
$val ||= 0
$val
Truth table50%1/2
A
A= $val
Aresult
11
00
63  
64 3 if (0) {
65 unless(defined($prec)) {
66 if (abs($val) > 1.0) {
67 $prec = 2;
68 } else {
69 my $v2 = sprintf("%0.2f", $val);
70 if ($val eq $v2) { # if normal rep isn't same as 2-digit representation
71 $prec = 2;
72 } else {
73 my $diff = abs($v2 - $val);
74 my $log10 = floor(_log10(abs $diff));
75 $prec = List::Util::min(6, List::Util::max(2, -$log10 + 1));
76 #print "val: $val, v2: $v2, log10: $log10, prec: $prec\n";
77 }
78 }
79 }
80  
81 my $str = $formatter->format_price( $val || 0, $prec, $indicator ); # returns negatives as '-$100' (what?)
82 $str =~ s/ //g;
83 if ($prec > 2) {
84 #print "before: $str\n";
85 $str =~ s{(\.\d{2,}[1-9])0+$}{$1};
86 #print "after $str\n";
87 }
88 return $str;
89 } else {
90 3 ▶ $val ||= 0;
Condition50%1/2
$val ||= 0
l!l
30
MC/DC0%0/1
$val ||= 0
$val
Truth table50%1/2
A
A= $val
Aresult
11
00
91  
92 3 ▶ unless(defined($prec)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $prec) 3 0 3
93 3 ▶ if (abs($val) > 1000) {
Branch: 2 branches
BranchTrueFalseTotal
if (abs $val > 1000) { } 2 1 3
elsif (abs $val > 1) { } 0 1 1
94 2 $prec = 0;
95 } elsif (abs($val) > 1) {
96 0 $prec = 2;
97 } else {
98 1 my $v2 = sprintf("%0.2f", $val);
99 1 ▶ if ($val eq $v2) { # if normal rep isn't same as 2-digit representation
Branch: 1 branch
BranchTrueFalseTotal
if ($val eq $v2) { } 0 1 1
100 0 $prec = 2;
101 } else {
102 1 my $diff = abs($v2 - $val);
103 1 my $log10 = floor(_log10(abs $diff));
104 1 $prec = List::Util::min(6, List::Util::max(2, -$log10 + 1));
105 #print "val: $val, v2: $v2, log10: $log10, prec: $prec\n";
106 }
107 }
108 }
109  
110 3 ▶ my $str = $formatter->format_price( $val || 0, $prec, $indicator );
Condition50%1/2
$val || 0
l!l
30
MC/DC0%0/1
$val || 0
$val
Truth table50%1/2
A
A= $val
Aresult
11
00
111 3 $str =~ s/ //g;
112 3 ▶ if ($prec > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prec > 2) 0 3 3
113 #print "before: $str\n";
114 0 $str =~ s{(\.\d{2,}[1-9])0+$}{$1};
115 #print "after $str\n";
116 }
117 3 return $str;
118 }
119 }
120  
121 1 ▶sub _log10 { my $n = shift; return 2 unless $n; return log($n)/log(10); }
Subroutine _log10: called CC=2 SCAR=8.1
Branch: 1 branch
BranchTrueFalseTotal
unless $n 1 0 1
122  
123 1 1;
124  
125  
126  
127  
128 #################### pod generated by Pod::Autopod - keep this line to make pod updates possible ####################
129  
130 =head1 NAME
131  
132 Finance::CoinbasePro::API::CLI::Util::CurrencyUtil
133  
134  
135 =head1 DESCRIPTION
136  
137 our global number formatter.
138  
139  
140 =head1 REQUIRES
141  
142 L<Number::Format>
143  
144 L<POSIX>
145  
146  
147 =head1 IMPLEMENTS
148  
149 L<Exporter>
150  
151  
152 =head1 METHODS
153  
154 =head2 format_crypto
155  
156 $self->format_crypto();
157  
158 =head2 format_crypto_exact
159  
160 $self->format_crypto_exact();
161  
162 =head2 format_crypto_with_usd
163  
164 $self->format_crypto_with_usd();
165  
166 =head2 format_currency
167  
168 $self->format_currency();
169  
170 =head2 format_fiat_currency
171  
172 $self->format_fiat_currency();
173  
174 =head2 format_usd
175  
176 $self->format_usd();
177  
178 =head2 get_product_currencies
179  
180 $self->get_product_currencies();
181  
182 =cut
183  
« CLI/Util/DateUtil.pm ↑ Summary CLI/Converter.pm »