Posted on Friday 25 February 2005
The wonderful sprintf function, which first appeared in C, often-used in PHP, allows you to format numbers, strings and dates very easily. Nate Cook implemented a version in Actionscript 1.0 a while ago, and I noticed it didn't work in AS2, so I created a wrapper class that Nate very quickly posted on his site. Example usage:
import com.natecook.Sprintf;
var myDate = new Date();
Sprintf.trace('%02d-%02d-%4d',
myDate.getMonth() + 1,
myDate.getDate(),
myDate.getFullYear()));
// traces out: 02-14-2004
Sprintf.trace('%.4f', Math,PI);
// traces out: 3.1416
You get two static methods, Sprintf.format and Sprintf.trace which are equivalent to the original sprintf(...) and trace(sprintf(...)), respectively. As for flags, look at the function reference on PHP.net.


