line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Template::Sandbox::NumberFunctions; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
8075
|
use strict; |
|
3
|
|
|
|
|
10
|
|
|
3
|
|
|
|
|
120
|
|
4
|
3
|
|
|
3
|
|
16
|
use warnings; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
109
|
|
5
|
|
|
|
|
|
|
|
6
|
3
|
|
|
3
|
|
16
|
use base 'Template::Sandbox::Library'; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
1179
|
|
7
|
|
|
|
|
|
|
|
8
|
3
|
|
|
3
|
|
19
|
use Template::Sandbox qw/:function_sugar/; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
2345
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
$Template::Sandbox::NumberFunctions::VERSION = '1.04'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
# From perlfaq5: add thousands-commas to number. |
13
|
|
|
|
|
|
|
# Yes it doesn't respect locale. |
14
|
|
|
|
|
|
|
sub _commify |
15
|
|
|
|
|
|
|
{ |
16
|
7
|
|
|
7
|
|
14
|
local $_ = shift; |
17
|
7
|
|
|
|
|
92
|
1 while s/^([-+]?\d+)(\d{3})/$1,$2/o; |
18
|
7
|
|
|
|
|
37
|
return $_; |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
__PACKAGE__->set_library_functions( |
22
|
|
|
|
|
|
|
int => ( one_arg sub { int( $_[ 0 ] ) } ), |
23
|
|
|
|
|
|
|
round => ( one_arg |
24
|
|
|
|
|
|
|
sub |
25
|
|
|
|
|
|
|
{ |
26
|
|
|
|
|
|
|
( int( $_[ 0 ] * 10 ) % 10 >= 5 ) ? |
27
|
|
|
|
|
|
|
( int( $_[ 0 ] ) + 1 ) : |
28
|
|
|
|
|
|
|
int( $_[ 0 ] ) |
29
|
|
|
|
|
|
|
} ), |
30
|
|
|
|
|
|
|
abs => ( one_arg sub { abs( $_[ 0 ] ) } ), |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# Pretty numeric formatting. |
33
|
|
|
|
|
|
|
numeric => ( one_arg \&_commify ), |
34
|
|
|
|
|
|
|
currency => ( one_arg |
35
|
|
|
|
|
|
|
sub { _commify( sprintf( '%.2f', $_[ 0 ] ) ) } ), |
36
|
|
|
|
|
|
|
accountant_currency => ( one_arg |
37
|
|
|
|
|
|
|
sub |
38
|
|
|
|
|
|
|
{ |
39
|
|
|
|
|
|
|
( $_[ 0 ] < 0 ) ? |
40
|
|
|
|
|
|
|
( '(' . _commify( sprintf( '%.2f', abs( $_[ 0 ] ) ) ) . ')' ) : |
41
|
|
|
|
|
|
|
( _commify( sprintf( '%.2f', $_[ 0 ] ) ) ) |
42
|
|
|
|
|
|
|
} ), |
43
|
|
|
|
|
|
|
decimal => ( two_args sub { sprintf( '%.' . $_[ 1 ] . 'f', $_[ 0 ] ) } ), |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
exp => ( one_arg sub { exp( $_[ 0 ] ) } ), |
46
|
|
|
|
|
|
|
log => ( one_arg sub { log( $_[ 0 ] ) } ), |
47
|
|
|
|
|
|
|
pow => ( two_args sub { $_[ 0 ] ** $_[ 1 ] } ), |
48
|
|
|
|
|
|
|
sqrt => ( one_arg sub { sqrt( $_[ 0 ] ) } ), |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
rand => ( one_arg inconstant |
51
|
|
|
|
|
|
|
sub |
52
|
|
|
|
|
|
|
{ |
53
|
|
|
|
|
|
|
ref( $_[ 0 ] ) ? |
54
|
|
|
|
|
|
|
$_[ 0 ]->[ int( rand( $#{$_[ 0 ]} + 1 ) ) ] : |
55
|
|
|
|
|
|
|
rand( $_[ 0 ] ) |
56
|
|
|
|
|
|
|
} ), |
57
|
|
|
|
|
|
|
srand => ( one_arg inconstant sub { srand( $_[ 0 ] ) } ), |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
max => ( two_args sub { $_[ 0 ] > $_[ 1 ] ? $_[ 0 ] : $_[ 1 ] } ), |
60
|
|
|
|
|
|
|
min => ( two_args sub { $_[ 0 ] < $_[ 1 ] ? $_[ 0 ] : $_[ 1 ] } ), |
61
|
|
|
|
|
|
|
); |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
__PACKAGE__->set_library_tags( |
64
|
|
|
|
|
|
|
'maths' => [ qw/exp log pow sqrt/ ], |
65
|
|
|
|
|
|
|
'display' => [ qw/numeric currency accountant_currency decimal/ ], |
66
|
|
|
|
|
|
|
); |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
1; |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
__END__ |