Finally, a printf function in AutoHotkey that is almost the same as printf functions in other languages.
I impressed myself with this one, but there is still room for improvement to fully implement all functionality as documented here
http://www.cplusplus.com/reference/clibrary/cstdio/printf/.
It would be nice to find an elegant way to pass the padchar value (currently defaulted to space) and a way to indicate "allow trunctation" (currently defaulted to no truncation).
As it is, it is very functional...
Feedback encouraged and welcome.

Example usage:
Code:
outputdebug DBGVIEWCLEAR ; view output in dbgview utility from microsoft at www.sysinternals.com
outputdebug % " ID FirstName LastName Number Code"
outputdebug % "---- ---------- ---------- ------- ----"
outputdebug % printf("%-4s %10s %10s %7f %4s", 23, "Alan", "Lilly", 3.999, "A")
outputdebug % printf("%-4s %10s %10s %-7.3f %4s this is amazing", 23, "Joe", "Apples", 3.999, "B")
outputdebug % printf("%-4s %10s %10s %07.0f %4s", 23, "Thomas", "Sterling", 3.999, "C")
Example formatting numbers:
outputdebug % printf("%07.1f", 145.09) ; round 1 decimal place and zerofill 7 digits
exitapp
Library Function printf:
Code:
;============================================================
; ahk printf function:
;
; closely emulates perl and c++ printf
; currently supports string and float
;
; string format:
; - to right justify
;
; float format:
; - for right justify
; 0 to indicate zerofill
;
; currently the pad character is hard coded as a space until I can find a more elegant way to pass it in
; also truncation does not occur (it would be nice to make this an option)
;============================================================
printf(string, prms*) ; uses variadics to handle variable number of inputs
{
padchar := " "
for each, prm in prms
{
RegExMatch(string,"`%(.*?)([s|f])",m)
format := m1
type := m2
if (type = "f") { ; format float using setformat command
originalformat := A_FormatFloat
SetFormat, Float, %format%
prm += 0.0
SetFormat, Float, %originalformat%
} else if (type = "s") { ; format string (pad string if necessary, negative number indicates right justify)
if (format < 0)
loop % -format-StrLen(prm)
prm := padchar prm
else
loop % format-StrLen(prm)
prm := prm padchar
} else {
msgbox, unknown type = %type% specified in call to printf
}
StringReplace, string, string, % "`" m, % prm ; "%" symbol must be escaped with backtick
}
return string
}