On Fri, 22 Nov 2002 21:21:42 -0800, [EMAIL PROTECTED] (John W. Krahn)
wrote:
>Chris wrote:
>>
>> How do I tally money with perl?
>
>Assuming that you are tallying dollars like $12,345.67, convert to cents
>(an integer) by removing all punctuation and convert back to dollars to
>print the final total.
>
>$ perl -le'
>@dollars = qw/ $12,345.67 $11,555.99 $9,765.35 $432 $876.4 /;
>for ( @dollars ) {
> # add zeros for proper format
> $_ .= ".00" unless tr/.//;
> $_ .= "0" unless /\.\d\d$/;
> # remove non-digits
> tr/0-9//cd;
> $total += $_;
> }
>($total2 = $total) =~ s/(\d\d)$/.$1/;
>printf "%.2f\n", $total / 100;
>print $total2;
>'
>34975.41
>34975.41
>
>
>
>John
OK. I will try it. My amounts look more like this.
@dollars = qw/ 12345.67 -12345.67 11555.993333 -11555.993333 9765.353
-9765.353 432 -432 876.4 -876.4/;
On another note
How do I stop the warning message
Possible attempt to separate words with commas at
@dollars = qw/ $12,345.67 $11,555.99 $9,765.35 $432 $876.4 /;
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]