Vladdy
07-28-2003, 07:25 PM
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.
MotherNatrsSon
07-28-2003, 07:40 PM
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
Vladdy
07-28-2003, 07:46 PM
You need to add some arguments:
printf('Integer: %i',23)
printf('',) will sure cause a syntax error.
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)
I can already see some uses for this script.
instead of
document.write"<div style='position:absolute;width:"+w+";height:"+h+"'></div>");
It would become
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
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. :thumbsup:
(Can you now do scanf?
scanf(elem.style.fontColor, "#%2x%2x%2x", &r, &g, &b)
:D )
Still playing...
Could be very usefull for those wanting to format currency. Try typing this into the test line
str="";for(i=0 ; i<10 ; i++) {money=Math.random()*400;str += (printf("$ %6.02f", money) + "\n" );}str;
:thumbsup:
Vladdy
07-29-2003, 05:26 PM
I'm glad you found it useful. :)
as far as scanf goes... cant do &r :D