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