Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 7th, 2000, 07:15 PM
bydavid bydavid is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2000
Location: USA
Posts: 67 bydavid User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old May 7th, 2000, 08:56 PM
esconsult esconsult is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2000
Posts: 43 esconsult User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #3  
Old May 7th, 2000, 09:10 PM
freebsd
Guest
Dev Shed Newbie (0 - 499 posts)
 
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

Reply With Quote
  #4  
Old May 8th, 2000, 03:21 AM
donarb donarb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 1999
Location: Seattle
Posts: 133 donarb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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).]

Reply With Quote
  #5  
Old May 8th, 2000, 06:39 AM
Shiju Rajan's Avatar
Shiju Rajan Shiju Rajan is offline
.Net Developer
Dev Shed Novice (500 - 999 posts)
 
Join Date: Feb 2000
Location: London
Posts: 987 Shiju Rajan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 26 m 22 sec
Reputation Power: 14
Send a message via MSN to Shiju Rajan Send a message via Yahoo to Shiju Rajan
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

Reply With Quote
  #6  
Old May 8th, 2000, 05:56 PM
bydavid bydavid is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2000
Location: USA
Posts: 67 bydavid User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #7  
Old May 8th, 2000, 05:57 PM
bydavid bydavid is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2000
Location: USA
Posts: 67 bydavid User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #8  
Old May 8th, 2000, 05:58 PM
bydavid bydavid is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2000
Location: USA
Posts: 67 bydavid User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #9  
Old May 8th, 2000, 06:00 PM
bydavid bydavid is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2000
Location: USA
Posts: 67 bydavid User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #10  
Old May 8th, 2000, 11:26 PM
freebsd
Guest
Dev Shed Newbie (0 - 499 posts)
 
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.

Reply With Quote
  #11  
Old May 9th, 2000, 12:54 AM
Shiju Rajan's Avatar
Shiju Rajan Shiju Rajan is offline
.Net Developer
Dev Shed Novice (500 - 999 posts)
 
Join Date: Feb 2000
Location: London
Posts: 987 Shiju Rajan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 26 m 22 sec
Reputation Power: 14
Send a message via MSN to Shiju Rajan Send a message via Yahoo to Shiju Rajan
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

Reply With Quote
  #12  
Old May 14th, 2000, 01:14 PM
donarb donarb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 1999
Location: Seattle
Posts: 133 donarb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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).]

Reply With Quote
  #13  
Old June 2nd, 2000, 08:59 PM
pschon pschon is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Posts: 16 pschon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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


Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > making comma's show up in numbers

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap