I'm trying to take a string, which may or may not contain a $ and/or
a comma, and return just the number with 2 decimal places. I am
having some trouble. Here is my test code:

$a[0]="$1,234.05";
$a[1]="1234.78";
$a[2]="$768";
$a[3]="777.99";

for ($j=0; $j<=3; $j++)
 {
 print "$j. " . Stripprice($a[$j]) . "\n";
 }
exit;

######################## end of main ###############################

# In: a string possibly containing a dollar sign and commas.
# Out: a number with 2 decimal places.
sub Stripprice
{my($newpr,$i,$c,$inpr);
$inpr=$_[0]; # price passed in

$i=0;
$newpr="";
#while ($i<=len($inpr))
# {
# $c=substr($inpr,$i,1);
# if ($c =~ m/\d/ or $c eq '.')
#  {
#  $newpr.=$c;
#  }
# $i++;
# } # while
$inpr=~ s/\$//g;
$inpr=~ s/\,//g;
$newpr=$inpr;

return $newpr;

My output is:
0. 234.05
1. 1234.78
2.
3. 777.99

For case 0, I think the $1 in the string has something to do with
it. But what happened with case 2?

Thanks.



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to