I have a subroutine that's outputting a float correctly, however when added to a value the last return gives a Euler's number for some reason. Does anyone know why this is and how can I just get the 0 that I'm expecting? Below is my code:
use strict;
use warnings;
my @vals = (
'00000000{',
'00000000{',
'00000000{',
'00000369I',
'00000020{',
'00000000{',
'00000100}',
'00000289R',);
my $total;
foreach (@vals) {
$total += calc_total($_);
print "\$total: $total\n";
}
sub calc_total {
my $val = shift;
my %pos_map = ('A' => 1, 'B' => 2, 'C' => 3, 'D' => 4, 'E' => 5, '
+F' => 6,
'G' => 7, 'H' => 8, 'I' => 9, '{' => 0);
my %neg_map = ('J' => 1, 'K' => 2, 'L' => 3, 'M' => 4, 'N' => 5, '
+O' => 6,
'P' => 7, 'Q' => 8, 'R' => 9, '}' => 0);
my $outval = substr($val, 0, 8);
if (exists($pos_map{substr($val, -1, 1)})) {
$outval .= $pos_map{substr($val, -1, 1)};
} elsif (exists($neg_map{substr($val, -1, 1)})) {
$outval .= $neg_map{substr($val, -1, 1)};
$outval *= -1;
} else {
print "Could not map total for: $val\n";
}
print "in the sub: " . sprintf("%.2f", $outval/100) . "\n";
return sprintf("%.2f", $outval/100);
}
I'm using Perl 5.16, too.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.