Revision f63cf9c9
Von Sven Schöling vor fast 3 Jahren hinzugefügt
SL/Helper/Number.pm | ||
---|---|---|
9 | 9 |
_format_number _round_number |
10 | 10 |
_format_total _round_total |
11 | 11 |
_parse_number |
12 |
_format_number_units |
|
12 | 13 |
); |
13 | 14 |
our %EXPORT_TAGS = (ALL => \@EXPORT_OK); |
14 | 15 |
|
... | ... | |
50 | 51 |
$amount; |
51 | 52 |
} |
52 | 53 |
|
54 |
sub _format_number_units { |
|
55 |
my ($amount, $places, $unit_from, $unit_to, %params) = @_; |
|
56 |
|
|
57 |
my $all_units = $params{all_units} //= SL::DB::Manager::Unit->get_all; |
|
58 |
|
|
59 |
if (!$unit_from || !$unit_to) { |
|
60 |
return _format_number($amount, $places, %params); |
|
61 |
} |
|
62 |
|
|
63 |
$amount *= $unit_from->convert_to(1, $unit_to); |
|
64 |
|
|
65 |
my $conv_units = $unit_from->convertible_units($all_units); |
|
66 |
|
|
67 |
if (!scalar @{ $conv_units }) { |
|
68 |
return _format_number($amount, $places, %params) . " " . $unit_to->name; |
|
69 |
} |
|
70 |
|
|
71 |
my @values; |
|
72 |
my $num; |
|
73 |
|
|
74 |
for my $unit (@$conv_units) { |
|
75 |
my $last = $unit->name eq $unit_to->name; |
|
76 |
if (!$last) { |
|
77 |
$num = int($amount / $unit->factor); |
|
78 |
$amount -= $num * $unit->factor; |
|
79 |
} |
|
80 |
|
|
81 |
if ($last ? $amount : $num) { |
|
82 |
push @values, { |
|
83 |
unit => $unit->name, |
|
84 |
amount => $last ? $amount / $unit->factor : $num, |
|
85 |
places => $last ? $places : 0 |
|
86 |
}; |
|
87 |
} |
|
88 |
|
|
89 |
last if $last; |
|
90 |
} |
|
91 |
|
|
92 |
if (!@values) { |
|
93 |
push @values, { "unit" => $unit_to->name, |
|
94 |
"amount" => 0, |
|
95 |
"places" => 0 }; |
|
96 |
} |
|
97 |
|
|
98 |
return join " ", map { |
|
99 |
_format_number($_->{amount}, $_->{places}, %params), $_->{unit} |
|
100 |
} @values; |
|
101 |
} |
|
102 |
|
|
53 | 103 |
sub _round_number { |
54 | 104 |
my ($amount, $places, $adjust) = @_; |
55 | 105 |
|
Auch abrufbar als: Unified diff
Helper::Number: _format_number_units
SL::Helper::Number pendant zu SL::Form::format_amount_units