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.

 Dev Shed Forums Sponsor:
#1
May 7th, 2000, 07:15 PM
 bydavid
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

#2
May 7th, 2000, 08:56 PM
 esconsult
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

#3
May 7th, 2000, 09:10 PM
 freebsd 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

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

#4
May 8th, 2000, 03:21 AM
 donarb
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).]

#5
May 8th, 2000, 06:39 AM
 Shiju Rajan
.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
}

<<tell me if you can simplify this script>>.

------------------
SR -
shiju.dreamcenter.net

#6
May 8th, 2000, 05:56 PM
 bydavid
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.

#7
May 8th, 2000, 05:57 PM
 bydavid
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

#8
May 8th, 2000, 05:58 PM
 bydavid
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.

#9
May 8th, 2000, 06:00 PM
 bydavid
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

#10
May 8th, 2000, 11:26 PM
 freebsd 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 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.

#11
May 9th, 2000, 12:54 AM
 Shiju Rajan
.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

#12
May 14th, 2000, 01:14 PM
 donarb
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).]

#13
June 2nd, 2000, 08:59 PM
 pschon
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

 Viewing: Dev Shed Forums > Programming Languages > Perl 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 Linear Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 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 Please select one User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home -------------------- Programming Languages    PHP Development        PHP FAQs and Stickies    Perl Programming        Perl FAQs and Stickies    C Programming        C Programming FAQs and Stickies    Java Help        Java FAQs    Python Programming        Python Programming FAQs    Ruby Programming        Ruby Programming FAQs    Game Development        Game Development FAQs Programming Languages - More    ASP Programming        ASP Programming FAQs    .Net Development        .Net Development FAQs    Visual Basic Programming        Visual Basic Programming FAQs    Software Design        Software Design FAQs    ColdFusion Development        ColdFusion Development FAQs    Delphi Programming        Delphi Programming FAQs    Regex Programming        Regex Programming FAQs    XML Programming        XML Programming FAQs    Other Programming Languages        Other Programming Languages FAQs Web Design    HTML Programming        HTML Programming FAQs    JavaScript Development        JavaScript Development FAQs    CSS Help        CSS Help FAQs    Flash Help        Flash Help FAQs    Photoshop Help        Photoshop Help FAQs    Web Design Help        Web Design Help FAQs    Website Critiques        Website Critiques FAQs    Search Engine Optimization        Search Engine Optimization FAQs Mobile Programming    Mobile Programming        Mobile Programming FAQs    iPhone SDK Development        iPhone SDK Development FAQs    Android Development        Android Development FAQs    BlackBerry Development        BlackBerry Development FAQs Web Site Management    Business Help        Business Help FAQs    Development Software        Development Software FAQs    Scripts        Scripts FAQs Databases    Database Management        Database Management FAQs    DB2 Development        DB2 Development FAQs    MySQL Help        MySQL Help FAQs    PostgreSQL Help        PostgreSQL Help FAQs    Firebird SQL Development        Firebird SQL Development FAQs    MS SQL Development        MS SQL Development FAQs    Oracle Development        Oracle Development FAQs    LDAP Programming        LDAP Programming FAQs System Administration    Mail Server Help        Mail Server Help FAQs    Apache Development        Apache Development FAQs    Security and Cryptography        Security and Cryptography FAQs    Antivirus Protection        Antivirus Protection FAQs    DNS        DNS FAQs    IIS        IIS FAQs    Networking Help        Networking Help FAQs    FTP Help        FTP Help FAQs Operating Systems    BSD Help        BSD Help FAQs    Linux Help        Linux Help FAQs    UNIX Help        UNIX Help FAQs    Windows Help        Windows Help FAQs    Mac Help        Mac Help FAQs Web Hosting    Web Hosting        Web Hosting FAQs    Free Web Hosting        Free Web Hosting FAQs    Web Hosting Requests        Web Hosting Requests FAQs    Web Hosting Offers        Web Hosting Offers FAQs Computer Hardware    Computer Hardware    CPUs        CPUs FAQs    Cooling        Cooling FAQs    Embedded Programming        Embedded Programming FAQs    Motherboards        Motherboards FAQs    Multimedia Hardware        Multimedia Hardware FAQs Other    Dev Shed Lounge        Dev Shed Lounge FAQs    Development Articles        Development Articles FAQs    Beginner Programming        Beginner Programming FAQs    Hire A Programmer        Hire A Programmer FAQs    Project Help Wanted        Project Help Wanted FAQs Latest News Updated Hourly    Technology News    Business News    Science News Forum Information    Forum Rules/Guidelines        Forum Rules/Guidelines FAQs    Forum Announcements        Forum Announcements FAQs    Dev Shed Gaming Center        Go to the Dev Shed Battle Arena        Go to the Dev Shed Arcade Games        Go to the Legend of the Green Dragon    Suggestions & Feedback        Suggestions & Feedback FAQs

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