# sprintf

Definition: | string s = sprintf(string fmt, object args)
Description: | This is exactly the same as printf() except that the output is returned as a string, rather than being sent to a file or device. fmt: a format string args: the object(s) to be printed. printf(fn, st, x) is equivalent to puts(fn, sprintf(st, x)) and in fact that is almost exactly how it is implemented. (In practice, printf() and sprintf() both call a private sprintf_() routine, to even up error handling.)
pwa/p2js: | Supported, with some minor differences, anticipate about 97%1 identical results, see also ?. 1I made that up. When/if I notice differences, I will either replace use of JavaScript primitives with hand transpiled code from builtins\VM\pprntfN.e or perhaps just list such minor discrepancies here.
Comments: | Some typical uses of sprintf() are: 1. Converting numbers to strings. 2. Creating strings to pass to system(). 3. Creating formatted error messages that can be passed to a common error message handler.
Example: |
    s = sprintf("%08d", 12345) -- s is "00012345"
Implementation: | See builtins\VM\pprntfN.e (an autoinclude) for details of the actual implementation, there is also a completely different hand-crafted version in p2js.js.
See Also: | printf, value, sprint, get, system