Gossamer Forum
Home : General : Perl Programming :

Parsing a number with ,,,, 's

Quote Reply
Parsing a number with ,,,, 's
I would like to be able to take a number, say from a file - like 32949 and make it into 32,949 . I don't want to write the file like that, I just want to be able to display the number like that.

Also, being able to turn 3999349 into 3,999,349 would also help.

Thanks in advance.
Quote Reply
Re: Parsing a number with ,,,, 's In reply to
Disregard, I posted in error.

[This message has been edited by Bobsie (edited August 08, 1999).]
Quote Reply
Re: Parsing a number with ,,,, 's In reply to
This code will do the work:

Code:
#!/usr/bin/perl
$number = "3999349";
(@numbers) = split(//,$number);
@numbers = reverse(@numbers);

$count = 0;
for (@numbers) {
if ($count%3 == 0 && $count != 0) {
$thenumber = "," . $thenumber;
}
$thenumber = $_ . $thenumber;
$count++;
}
print "content-type: text/plain\n\n";
print $thenumber;

Output: 3,999,349

Regards,
Federico

--------------
UBBdir.com: Directory of UBBs (forums)
www.UBBdir.com

[This message has been edited by polpus (edited August 08, 1999).]