README
A complete implementation of the printf
C functions family
for Node.JS, written in pure JavaScript.
Bonus! You get extra features, like the %O
converter (which inspect
s
the argument). See Extra Features below.
Installing
Via NPM:
$ npm install printf
Usage
Use it like you would in C (printf
/sprintf
):
var printf = require('printf');
var result = printf(format, args...);
It can also output the result for you, as fprintf
:
var printf = require('printf');
printf(write_stream, format, args...);
Features
(space)
Flag assert.eql(' -42', printf('% 5d', -42));
+
(plus)
Flag assert.eql(' +42', printf('%+5d', 42));
0
(zero)
Flag assert.eql('00042', printf('%05d', 42));
-
(minus)
Flag assert.eql('42 ', printf('%-5d', 42));
Width / precision
assert.eql('42.90', printf('%.2f', 42.8952));
assert.eql('042.90', printf('%06.2f', 42.8952));
Numerical bases
assert.eql('\x7f', printf('%c', 0x7f));
assert.eql('a', printf('%c', 'a'));
assert.eql('"', printf('%c', 34));
Miscellaneous
assert.eql('10%', printf('%d%%', 10));
assert.eql('+hello+', printf('+%s+', 'hello'));
assert.eql("a", printf("%c", "a"));
assert.eql('"', printf("%c", 34));
assert.eql('