insta.gator has asked for the wisdom of the Perl Monks concerning the following question:
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 mathThe 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!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Proper creation of a negative number
by toolic (Bishop) on Jul 08, 2015 at 13:31 UTC | |
by Anonymous Monk on Jul 08, 2015 at 17:12 UTC | |
|
Re: Proper creation of a negative number
by kcott (Archbishop) on Jul 08, 2015 at 15:24 UTC | |
by AnomalousMonk (Archbishop) on Jul 09, 2015 at 01:46 UTC | |
by kcott (Archbishop) on Jul 09, 2015 at 03:59 UTC | |
|
Re: Proper creation of a negative number
by pme (Monsignor) on Jul 08, 2015 at 13:40 UTC |