Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8

Thread: printf()

  1. #1
    Senior Coder
    Join Date
    Jun 2002
    Location
    Nashua, NH
    Posts
    1,724
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile printf()

    Any of you old C wolves ever feel nostalgic about good, old, trusted printf()?
    I did, and since when it comes to JavaScript I write first and Google later, here is what I came up with:
    www.vladdy.net/Demos/printf.html

    Integer length modifiers and pointer types are not supported in my emulation for obvious reasons.

    Bugs are quite possible - bug reports are quite welcome.

    WA: I thought the script is too long to post inline. If you think different, I would be happy to follow the category guidelines and correct my mistake.
    Last edited by Vladdy; 07-28-2003 at 07:31 PM.
    Vladdy | KL
    "Working web site is not the one that looks the same on common graphical browsers running on desktop computers, but the one that adequately delivers information regardless of device accessing it"

  • #2
    Senior Coder
    Join Date
    Mar 2003
    Location
    OHIO
    Posts
    1,438
    Thanks
    1
    Thanked 0 Times in 0 Posts
    It does not work for me in Netscape 7 on a Mac. I get this error from the javascript console:

    Error: syntax error
    Source Code:
    printf('',);

    MNS
    [size=1]"If you want to be "in the biz" you are going to have to roll with the changes or get out, basically."

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    Nashua, NH
    Posts
    1,724
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You need to add some arguments:
    printf('Integer: %i',23)

    printf('',) will sure cause a syntax error.
    Vladdy | KL
    "Working web site is not the one that looks the same on common graphical browsers running on desktop computers, but the one that adequately delivers information regardless of device accessing it"

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    Atlanta, GA.
    Posts
    313
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ahh,
    reminds me of my 'C' programming days.

    Seems to work. Tried the hex "%09x" and decimal "%09d" and it worked ok untill I did

    printf("images/img%03d.gif", 1); => images/img001.gif
    printf("images/img%03d.gif", 2); => images/img002.gif
    printf("images/img%03d.gif", 3); => images/img003.gif

    but then

    printf("images/img%03d.gif", 0); => images/img.gif

    (same for %03x)

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    Nashua, NH
    Posts
    1,724
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Nice catch, Roy!
    Fixed.
    Vladdy | KL
    "Working web site is not the one that looks the same on common graphical browsers running on desktop computers, but the one that adequately delivers information regardless of device accessing it"

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Location
    Atlanta, GA.
    Posts
    313
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I can already see some uses for this script.

    instead of
    Code:
    document.write"<div style='position:absolute;width:"+w+";height:"+h+"'></div>");
    It would become
    Code:
    var w=200;
    var h=100;
    printf("<div style='position:absolute;width:%d;height%d'></div>", w,h);
    Output:
    <div style='position:absolute;width:200;height100'></div>


    Which seems a lot easier to read.

    And I tried this and it worked

    Code:
    var red=128;
    var green=255;
    var blue=127;
    
    if(moz)
        printf("font-color: rgb(%d %d %d); ", red, green, blue);
    else
        printf("font-color:#%2x%2x%2x;", red, green, blue);
    Output:
    font-color: rgb(128 255 127);
    or
    font-color:#80ff7f;

    A very nice utility function, thanks for sharing.
    (Can you now do scanf?
    scanf(elem.style.fontColor, "#%2x%2x%2x", &r, &g, &b)
    )

  • #7
    Regular Coder
    Join Date
    Jun 2002
    Location
    Atlanta, GA.
    Posts
    313
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Still playing...

    Could be very usefull for those wanting to format currency. Try typing this into the test line
    Code:
    str="";for(i=0 ; i<10 ; i++) {money=Math.random()*400;str += (printf("$ %6.02f", money)  + "\n" );}str;

  • #8
    Senior Coder
    Join Date
    Jun 2002
    Location
    Nashua, NH
    Posts
    1,724
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm glad you found it useful.
    as far as scanf goes... cant do &r
    Vladdy | KL
    "Working web site is not the one that looks the same on common graphical browsers running on desktop computers, but the one that adequately delivers information regardless of device accessing it"


  •  

    Posting Permissions

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