implement access to the cart for the new template tagging
[bse.git] / site / cgi-bin / modules / BSE / Util / Format.pm
CommitLineData
11af7272
TC
1package BSE::Util::Format;
2use strict;
3
4our $VERSION = "1.000";
5
6sub bse_number {
7 my ($format, $value, $cfg) = @_;
8
9 $cfg ||= BSE::Cfg->single;
10 my $section = "number $format";
11 my $comma_sep = $cfg->entry($section, "comma", ",");
12 $comma_sep =~ s/^"(.*)"$/$1/;
13 $comma_sep =~ /\w/ and return "* comma cannot be a word character *";
14 my $comma_limit = $cfg->entry($section, "comma_limit", 1000);
15 my $commify = $cfg->entry($section, "commify", 1);
16 my $dec_sep = $cfg->entry($section, "decimal", ".");
17 my $div = $cfg->entry($section, "divisor", 1)
18 or return "* divisor must be non-zero *";
19 my $places = $cfg->entry($section, "places", -1);
20
21 my $div_value = $value / $div;
22 my $formatted = $places < 0 ? $div_value : sprintf("%.*f", $places, $div_value);
23
24 my ($int, $frac) = split /\./, $formatted;
25 if ($commify && $int >= $comma_limit) {
26 1 while $int =~ s/([0-9])([0-9][0-9][0-9]\b)/$1$comma_sep$2/;
27 }
28
29 if (defined $frac) {
30 return $int . $dec_sep . $frac;
31 }
32 else {
33 return $int;
34 }
35}
36
371;