Thread Runden auf zwei relevante Stellen?!
(8 answers)
Opened by Ronnie at 2008-09-24 23:49
Etwas umständlich, aber es tut erstmal:
Code (perl): (dl
)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 sub to_three_sig_digits { my $val = shift; if ($val =~ /^(0\.0*)(\d+)$/) { my $pre = $1; my $app = $2; return $val if length($app) <= 3; my $digits = substr($app, 0, 3); my $fourth = substr($app, 3, 1); $digits += 1 if $fourth >= 5; $val = $pre . $digits; } elsif ($val > 1) { $val = sprintf("%.2f", $val); } return $val; } |