#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2000
    Location
    USA
    Posts
    67
    Rep Power
    15
    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. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Posts
    43
    Rep Power
    15
    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
  4. #3
  5. No Profile Picture
    freebsd
    Guest
    Devshed Newbie (0 - 499 posts)
    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//modu...at-1.41.readme
    Download it at: http://www.perl.com/CPAN-local//modu...at-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
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 1999
    Location
    Seattle
    Posts
    133
    Rep Power
    16
    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).]
  8. #5
  9. .Net Developer
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    London
    Posts
    987
    Rep Power
    15
    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
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2000
    Location
    USA
    Posts
    67
    Rep Power
    15
    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.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2000
    Location
    USA
    Posts
    67
    Rep Power
    15
    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
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2000
    Location
    USA
    Posts
    67
    Rep Power
    15
    shiju

    thanks again for the code and helping me out. i will try to simplify the code and return it to you.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2000
    Location
    USA
    Posts
    67
    Rep Power
    15
    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
  18. #10
  19. No Profile Picture
    freebsd
    Guest
    Devshed Newbie (0 - 499 posts)
    >>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.
  20. #11
  21. .Net Developer
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    London
    Posts
    987
    Rep Power
    15
    donarb,

    <<< 1 while $str =~ s/(.*d)(ddd)/$1,$2/;>>

    What does it do???

    I think it will add another "dot" to the string.

    ---------- &#0124; &#0124;| ----- &#0124; &#0124;| ----- &#0124; &#0124;| ---

    freebsd,Thank you very much for explainig about Format.pm module.




    ------------------
    SR -
    shiju.dreamcenter.net
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 1999
    Location
    Seattle
    Posts
    133
    Rep Power
    16

    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).]
  24. #13
  25. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    16
    Rep 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


IMN logo majestic logo threadwatch logo seochat tools logo