#1
  1. No Profile Picture
    tbonds
    Guest
    Devshed Newbie (0 - 499 posts)
    $tax = sprintf("$%5.2f", tax());

    ===================================
    The above expression returns "5.2f", instead
    of formatting the variable contents and returning a value like "$4.50". However, when I get rid of the "$" in the above expression it returns a formatted number, but does not place the dollar sign in front of the number.

    Am I missing something here? I've checked a few examples on this site and in my Perl books, but don't see what I'm doing wrong.

    Please help!! Thanks.
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2000
    Posts
    4
    Rep Power
    0

    Hello there,

    It's because $ is a special character in Perl. Try this line instead:

    $tax = "$" . sprintf("%5.2f", tax());

    Hope that helps,
    Mike
  4. #3
  5. No Profile Picture
    tbonds
    Guest
    Devshed Newbie (0 - 499 posts)
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by Envex:

    Hello there,

    It's because $ is a special character in Perl. Try this line instead:

    $tax = "$" . sprintf("%5.2f", tax());

    Hope that helps,
    Mike
    [/quote]

    If what you say is true, why don't the Perl books cover your alternate way of representing my statement? Even the DevShed section doesn't mention this. Anyway, thanks for your help.

  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2000
    Posts
    4
    Rep Power
    0

    If what I say is true?!? It's common sense. $ represents a scalar variable, so you can't just go throw it in anywhere.

    Mike


    ------------------
    Envex Developments
    Your CGI Script Specialists
    http://www.envex.net/
  8. #5
  9. No Profile Picture
    tbonds
    Guest
    Devshed Newbie (0 - 499 posts)
    So, printf("$%2.2f", 25.99) works OK, but sprintf("$%2.2f", 25.99) doesn't work OK? I tried your method and was successful. However, why would it work OK with printf, but not sprintf? Is there a difference? Seems inconsistent to me. I'm not doubting what you say, but I'm learning Perl through books and this Website's tutorials, so either there are type-o's in Devshed's tutorials or my Perl compiler is broken/outdated! Every example of the above uses the "$" within the formatting expression, yet I can't seem to use it that way and still get good results with my numbers.

    DevShed tutorial regarding the printf and sprintf fuctions can be found at: http://www.devshed.com/Server_Side/P...1_6/page6.html

    Regards,


  10. #6
  11. No Profile Picture
    freebsd
    Guest
    Devshed Newbie (0 - 499 posts)
    What you are trying to do already been replied at -> http://www.devshed.com/Talk/Forums/F...ML/000571.html

IMN logo majestic logo threadwatch logo seochat tools logo