- $amount ||= 0;
- $dash ||= '';
- my $neg = $amount < 0;
- my $force_places = defined $places && $places >= 0;
-
- $amount = $self->round_amount($amount, abs $places) if $force_places;
- $neg = 0 if $amount == 0; # don't show negative zero
- $amount = sprintf "%.*f", ($force_places ? $places : 10), abs $amount; # 6 is default for %fa
-
- # before the sprintf amount was a number, afterwards it's a string. because of the dynamic nature of perl
- # this is easy to confuse, so keep in mind: before this comment no s///, m//, concat or other strong ops on
- # $amount. after this comment no +,-,*,/,abs. it will only introduce subtle bugs.
-
- $amount =~ s/0*$// unless defined $places && $places == 0; # cull trailing 0s
-
- my @d = map { s/\d//g; reverse split // } my $tmp = $myconfig->{numberformat}; # get delim chars
- my @p = split(/\./, $amount); # split amount at decimal point
-
- $p[0] =~ s/\B(?=(...)*$)/$d[1]/g if $d[1]; # add 1,000 delimiters
- $amount = $p[0];
- if ($places || $p[1]) {
- $amount .= $d[0]
- . ( $p[1] || '' )
- . (0 x max(abs($places || 0) - length ($p[1]||''), 0)); # pad the fraction
- }
-
- $amount = do {
- ($dash =~ /-/) ? ($neg ? "($amount)" : "$amount" ) :
- ($dash =~ /DRCR/) ? ($neg ? "$amount " . $main::locale->text('DR') : "$amount " . $main::locale->text('CR') ) :
- ($neg ? "-$amount" : "$amount" ) ;
- };
-
- $main::lxdebug->leave_sub(2);
- return $amount;
-}
-
-sub format_amount_units {
- $main::lxdebug->enter_sub();
-
- my $self = shift;
- my %params = @_;
-
- my $myconfig = \%main::myconfig;
- my $amount = $params{amount} * 1;
- my $places = $params{places};
- my $part_unit_name = $params{part_unit};
- my $amount_unit_name = $params{amount_unit};
- my $conv_units = $params{conv_units};
- my $max_places = $params{max_places};
-
- if (!$part_unit_name) {
- $main::lxdebug->leave_sub();
- return '';
- }
-
- my $all_units = AM->retrieve_all_units;
-
- if (('' eq ref $conv_units) && ($conv_units =~ /convertible/)) {
- $conv_units = AM->convertible_units($all_units, $part_unit_name, $conv_units eq 'convertible_not_smaller');
- }
-
- if (!scalar @{ $conv_units }) {
- my $result = $self->format_amount($myconfig, $amount, $places, undef, $max_places) . " " . $part_unit_name;
- $main::lxdebug->leave_sub();
- return $result;
- }
-
- my $part_unit = $all_units->{$part_unit_name};
- my $conv_unit = ($amount_unit_name && ($amount_unit_name ne $part_unit_name)) ? $all_units->{$amount_unit_name} : $part_unit;
-
- $amount *= $conv_unit->{factor};
-
- my @values;
- my $num;
-
- foreach my $unit (@$conv_units) {
- my $last = $unit->{name} eq $part_unit->{name};
- if (!$last) {
- $num = int($amount / $unit->{factor});
- $amount -= $num * $unit->{factor};
- }
-
- if ($last ? $amount : $num) {
- push @values, { "unit" => $unit->{name},
- "amount" => $last ? $amount / $unit->{factor} : $num,
- "places" => $last ? $places : 0 };
- }
-
- last if $last;
- }
-
- if (!@values) {
- push @values, { "unit" => $part_unit_name,
- "amount" => 0,
- "places" => 0 };
- }
-
- my $result = join " ", map { $self->format_amount($myconfig, $_->{amount}, $_->{places}, undef, $max_places), $_->{unit} } @values;
-
- $main::lxdebug->leave_sub();
-
- return $result;