The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Perl Programming
|
making comma's show up in numbers
Discuss making comma's show up in numbers in the Perl Programming forum on Dev Shed. making comma's show up in numbers Perl Programming forum discussing coding in Perl, utilizing Perl modules, and other Perl-related topics. Perl, the Practical Extraction and Reporting Language, is the choice for many for parsing textual information.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

May 7th, 2000, 07:15 PM
|
|
Contributing User
|
|
Join Date: Mar 2000
Location: USA
Posts: 67
Time spent in forums: < 1 sec
Reputation Power: 14
|
|
|
I am trying to make numbers show up witht he commas in them , but i canot seem to find a print or function to do this.
I would like the function to do this ....
1234.00 = 1,234.00
If someone could show me how or tell me where to find a function to do this, i would appreciate it greatly.
Thank you in advanced
|

May 7th, 2000, 08:56 PM
|
|
Contributing User
|
|
Join Date: May 2000
Posts: 43
Time spent in forums: < 1 sec
Reputation Power: 14
|
|
There is no function to do it in Perl. I almost died trying to do it one day, until I found out the awful truth!
I converted this PHP function to Perl, can't find the perl version right now, but dragged this off the PHP site:
function Money($inbuffer, $dolsign = 0)
{
$buffer = sprintf("%0.2f", $inbuffer);
if(strlen($buffer) > 6)
{
$thenumber = "";
$decimal = substr($buffer, strrpos($buffer, "."));
$left = substr($buffer, 0, strrpos($buffer, "."));
while(strlen($left) > 3)
{
$thenumber = $thenumber . "," . substr($left, -3);
$left = substr($left, 0, strlen($left)-3);
}
$thenumber = $left . $thenumber . $decimal;
}
else
$thenumber = $buffer;
if($dolsign != 0)
$thenumber = "$ $thenumber";
return($thenumber);
}
------------------
PHP, Perl, SQL Programming at http://www.mentalobjects.com
|

May 7th, 2000, 09:10 PM
|
|
Guest
|
|
Posts: n/a
Time spent in forums:
Reputation Power:
|
|
|
There is almost nothing you can't do with Perl, it's a matter of efficiency and speed.
You can do it in Perl with perl module Format.pm installed.
The readme can be found at: http://www.perl.com/CPAN-local//modules/by-module/Number/Number-Format-1.41.readme
Download it at: http://www.perl.com/CPAN-local//modules/by-module/Number/Number-Format-1.41.tar.gz
Here is a quick example if you don't wanna waste time to read the readme file
##################################
#!/usr/local/bin/perl
$number = "1234567";
use Number::Format qw(:subs :vars);
$THOUSANDS_SEP = ',';
my $formatted = format_number($number);
print "$formattedn"; # will print 1,234,567
##################################
You can do more with Format.pm but it's beyond the scope for this reply. You need to figure out yourself.
#########To install Number-Format-1.41.tar.gz########
tar -zxvf Number-Format-1.41.tar.gz
cd Number-Format-1.41
perl Makefile.PL
make
make install
|

May 8th, 2000, 03:21 AM
|
|
Contributing User
|
|
Join Date: Jun 1999
Location: Seattle
Posts: 133
Time spent in forums: < 1 sec
Reputation Power: 14
|
|
|
1 while $str =~ s/(.*d)(ddd)/$1,$2/;
Note, this may break if the string contains non-digit characters. If you have a decimal in the string, I would break it apart at the decimal, run the comma insertion code, then reassemble the string.
Don
[This message has been edited by donarb (edited May 08, 2000).]
|

May 8th, 2000, 06:39 AM
|
 |
.Net Developer
|
|
Join Date: Feb 2000
Location: London
Posts: 987
Time spent in forums: 3 h 26 m 22 sec
Reputation Power: 14
|
|
Hi David,
i am writing a very small procedure for you to formatting a number.If you are not able to use perl module Format.pm then,you can try my script..
#!/usr/bin/perl
print &format_number("12345.00");;
sub format_number{
my $num=$_[0];
$dot_pos=rindex($num,".");
#find out dot position
$decimal_val=substr($num,$dot_pos,3) if $dot_pos !=-1;
#get the decimal value if dot is present
$num=substr($num,0,$dot_pos) if $dot_pos !=-1;
#get the digit portion value if dot is present
$num=reverse($num);
#reverse the number for assaining ","
for($a=0;$a<length($num);$a++){
# cut 3 digits each and assaign "," to it.
if($a=~ /0|3|6|9|12|15/){
if($a==0){
$number=substr($num,$a,3);
}else{
$number.=",".substr($num,$a,3);
}
}
}
return reverse($number).$decimal_val;
#reverse the number in normal form.
#return the value
}
hope this may help you..
<<tell me if you can simplify this script>>.
------------------
SR -
shiju.dreamcenter.net
|

May 8th, 2000, 05:56 PM
|
|
Contributing User
|
|
Join Date: Mar 2000
Location: USA
Posts: 67
Time spent in forums: < 1 sec
Reputation Power: 14
|
|
|
Freebsd,
thanks for the info on perl module for handling formats. since hte site resides on someone elses server, i do not know if i can install perl modules oon my own. i will try to though.
|

May 8th, 2000, 05:57 PM
|
|
Contributing User
|
|
Join Date: Mar 2000
Location: USA
Posts: 67
Time spent in forums: < 1 sec
Reputation Power: 14
|
|
|
econsultant
thanks also for the cod ethat you listed, i didnt realize that there is a substring f(x) in perl, where cna i find a list of common f(x) in perl so taht i can have a referenceae?
thanks
|

May 8th, 2000, 05:58 PM
|
|
Contributing User
|
|
Join Date: Mar 2000
Location: USA
Posts: 67
Time spent in forums: < 1 sec
Reputation Power: 14
|
|
|
shiju
thanks again for the code and helping me out. i will try to simplify the code and return it to you.
|

May 8th, 2000, 06:00 PM
|
|
Contributing User
|
|
Join Date: Mar 2000
Location: USA
Posts: 67
Time spent in forums: < 1 sec
Reputation Power: 14
|
|
|
donarb, im still fairly new to perl and i was wondering if you could elaborate on how to use the code that you gave...
thanks
|

May 8th, 2000, 11:26 PM
|
|
Guest
|
|
Posts: n/a
Time spent in forums:
Reputation Power:
|
|
|
>>i do not know if i can install perl modules oon my own
Not all the modules require installation on the server. I am not sure about Format.pm, but in some cases, you can simply upload it to your web account under any directory but you must put Format.pm into a directory called "Number".
Here is an example assuming the root directory of your web account is: /home/http/username/public_html
##################################
#!/usr/local/bin/perl
$number = "1234567";
# this is the line you needed
use lib '/home/http/username/public_html/lib';
use Number::Format qw(:subs :vars);
$THOUSANDS_SEP = ',';
my $formatted = format_number($number);
print "Content-type: text/htmlnn";
print "$formattedn"; # will print 1,234,567
##################################
In this case, you need to make a directory called "lib", then make a subdirectory called "Number" (case sensitive) under your "lib" directory. Next, upload Format.pm into "Number" directory.
|

May 9th, 2000, 12:54 AM
|
 |
.Net Developer
|
|
Join Date: Feb 2000
Location: London
Posts: 987
Time spent in forums: 3 h 26 m 22 sec
Reputation Power: 14
|
|
donarb,
<<< 1 while $str =~ s/(.*d)(ddd)/$1,$2/;>>
What does it do???
I think it will add another "dot" to the string.
---------- | || ----- | || ----- | || ---
freebsd,Thank you very much for explainig about Format.pm module.
------------------
SR -
shiju.dreamcenter.net
|

May 14th, 2000, 01:14 PM
|
|
Contributing User
|
|
Join Date: Jun 1999
Location: Seattle
Posts: 133
Time spent in forums: < 1 sec
Reputation Power: 14
|
|
|
1 while $str =~ s/(.*d)(ddd)/$1,$2/;
What does it do??? I think it will add another "dot" to the string.
No, the period means "any character". What the code does is look for four digits in a row. Then the $str is reassembled by substituting the $1,$2, inserting a comma between the first digit and the set of three digits. This keeps looping until there are no more substrings of four digits in a row.
Using an example $str = "123456789", the values after each loop are as follows:
loop1
$str= "123456,789"
$1 = "123456"
$2 = "789"
loop2
$str = "123,456,789"
$1 = "123"
$2 = "456"
Don
[This message has been edited by donarb (edited May 14, 2000).]
|

June 2nd, 2000, 08:59 PM
|
|
Junior Member
|
|
Join Date: Jun 2000
Posts: 16
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by bydavid:
I am trying to make numbers show up witht he commas in them , but i canot seem to find a print or function to do this.
I would like the function to do this ....
1234.00 = 1,234.00
[/quote]
No one posting in this Perl forum should be without the following book:
"Perl Cookbook"
pages 64,65, 2.17 Putting Commas in Numbers
This books contains over 700 pages of such recipes that one is always wondering about (before the book is worn out
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|