Hi Monks. I have a script that parses numeric values from a file. We have discovered that when a negative value exists in the file, it is displayed with the negative sign following the number rather than preceding the number. My script seems to be just ignoring the negative sign. Here is the code

@line = split (/\s+/, $line); # Split line on any amount of + whitespace $TFCNTL_Tax = @line[8]; $TFCNTL_Tax =~ s/(\$|,)//g; # Get rid of $ and comma +s push(@TFCNTL_Tax,$TFCNTL_Tax); # Save value for later print "TFCNTL_Tax=@TFCNTL_Tax\n\n";

So for example, the value of @line8 may be 7.89-. Later in the script we format the value as follows:

$total = sprintf("%.2f", @TFCNTL_Tax[$i]);  # Format number for floating point math

The result is that the negative number becomes a positive number.

I am assuming that we need to do some sort of test to determine if there is a minus sign following the number and then alter the variable to make it an actual negative number. Just not sure how to go about this. Can someone please help me with the best way to fix this?

Thanks!


In reply to Proper creation of a negative number by insta.gator

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.