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