SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Nov 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Why and when do we use printf and sprintf

    Hi every body. My question is why and when do we use ASCII. For example, why should I use % instead of \n. Deep explanation will be appreciated.

  2. #2
    SitePoint Zealot
    Join Date
    Oct 2008
    Posts
    167
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You use printf when you want to do formatting on a number. For example you may have a variable with the value of 4.45938458932848381273. You may only be interested in wanting to see two decimal places.

    PHP Code:
    <?php
    $var 
    4.45938458932848381273;
    printf("%.02f",$var);
    ?>
    will print out 4.46 because you just wanted the two decimal places. sprintf() is the same thing except it returns the formatted string as a string instead of outputting it.

    For more information on the formatting rules see:

    http://us2.php.net/manual/en/function.sprintf.php
    CanisHosting - Web Hosting plans starting at $3.95 per month

  3. #3
    SitePoint Member
    Join Date
    Nov 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for the quick respond, but why do we use ASCII for this case where we can use number_format().
    I see people using ASCII to perform the job of \n or \r. my question is why the ASCII.

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Why use a dozen commands where one can do the same thing. While using number_format can do that for you easily when you have one number to display, the printf command can handle a whole series of number that you want to output like that all in one call instead of one call per number.

    PHP Code:
    <?php
    $var1 
    4.45938458932848381273;
    $var23.1415926535897932;
    $var3 6.357852725752780;
    printf("%.02f is between %.02f and %.02f.",$var1$var2$var3);
    ?>
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I see people using ASCII to perform the job of \n or \r. my question is why the ASCII.
    Can you give an example?
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You mean like this?
    PHP Code:
    printf('make new line -->%c'12); 

  7. #7
    SitePoint Zealot
    Join Date
    Oct 2008
    Posts
    167
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ASCII(12) is a carriage return character. The use here is saying to use ASCII(12) at the end of the string, which does a carriage return.

    I don't know why you would use this, I guess just personal preference, but this would be the same thing as.

    PHP Code:
    print("make new line -->\n"); 
    EDIT: Woops, didn't see that this wasn't the OP with the example.
    CanisHosting - Web Hosting plans starting at $3.95 per month


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •