73

Looking to do output formatting (sprintf type functionality) in node.js, but before I write it myself I was wondering if there's something similar built-in (I've trawled the docs to no avail) or if someone's already written a module.

Many thanks

Jonas
  • 121,568
  • 97
  • 310
  • 388
  • I would suggest looking at [this SO thread](http://stackoverflow.com/questions/610406/javascript-printf-string-format) as well. – Husky Jul 02 '11 at 22:54
  • I have since released a fast and spec compliant printf implementation for Node.js and browser https://github.com/gajus/fast-printf – Gajus Jan 03 '21 at 02:53

6 Answers6

82

There is now printf-like support in util.format().

Example:

util.format('hello %s', 'world');
// Returns: 'hello world'
congusbongus
  • 13,359
  • 7
  • 71
  • 99
lapo
  • 3,136
  • 26
  • 34
  • For what it's worth: `function printf() { return process.stdout.write(util.format.apply(null, arguments)); } – Elliot Foster Jan 12 '12 at 17:41
  • 79
    Also, the support in `util.format` is very very basic: no `%5d` or `%5.3f` or anything like that, so it's not a real `sprintf`-like solution, unfortunately. – lapo Jan 13 '12 at 16:52
  • 3
    Similar to @Elliot Foster's comment, you could also do `var printf = require('util').format`; – Ian Oxley Mar 27 '12 at 16:28
  • Docs for node v0.8.2: http://nodejs.org/docs/v0.8.2/api/util.html#util_util_format_format – calvinf Jul 17 '12 at 18:03
  • 1
    @lapo floating point number can be rounded using toFixed() `var a = 1.234567;a.toFixed(3)` => `>'1.235'` – Etienne May 06 '14 at 20:37
  • 7
    it's useless, it's not printf-like at all – Pavel P Mar 12 '17 at 13:06
  • 4
    Coercing numbers into fixed width string representations, possibly with 0-padding, is a staple use case for printf-alikes; `util.format` does not support that (eg `require('util').format("blarf_%04d", 42);`) as of v 10.5.0. – collapsar Jul 06 '18 at 09:57
  • This is deprecated in current versions of node.js. – Preston L. Bannister Nov 18 '21 at 01:19
27

There are couple in the npm registry which are actual sprintf implementations since util.format has just a very basic support.

Gajus
  • 69,002
  • 70
  • 275
  • 438
KARASZI István
  • 30,900
  • 8
  • 101
  • 128
  • 1
    unfortunately sprintf-js is incompatible with colors: ```sprintf(" %s %s", title.grey, colors['blue'](msg)) ' \x1B[90mTitle\x1B[39m \x1B[34mMessage\x1B[39m'``` – scavenger Sep 13 '20 at 18:24
7

Here is the javascript version of sprintf:

http://phpjs.org/functions/sprintf:522

Sarfraz
  • 377,238
  • 77
  • 533
  • 578
5

console.log works fine.

console.log('%d hours', 4); // 4 hours
console.log('The %2$s contains %1$d monkeys', 4, 'tree'); // The tree contains 4 monkeys
Jürgen Paul
  • 14,299
  • 26
  • 93
  • 133
  • 4
    The first example works on Node 0.10.18, but the second completely fails: the 2$/1$ need to be removed to actually get parameter interpolation, and then the parameters need to be in correct order, otherwise you get: console.log('The %s contains %d monkeys', 4, 'tree'); returns: The 4 contains NaN monkeys ``` – FGM Sep 23 '13 at 09:37
  • The second example works in Chrome's (v33) console. Per @FGM, `console.log('The %s contains %d monkeys', 'tree', 4);` works in node v0.10.26. – joemaller Mar 26 '14 at 17:24
  • 3
    useful tip (if it works) but it's equivalent to `printf`, not `sprintf`. – Alnitak Jun 01 '15 at 12:21
  • 1
    not sure why it doesn't work on node v9.4, just use `\`es6 template ${format}\`` – Ray Foss Feb 02 '18 at 19:22
0

Use locutus

As of now, there is a package that translate functions of other languages to Javascript such as php, python, ruby etc.

const sprintf = require("locutus/php/strings/sprintf")
const data = sprintf("%01.3f", 2);
console.log(data)
//output: 2.000

Try it here codesandbox

ßiansor Å. Ålmerol
  • 2,849
  • 3
  • 22
  • 24
0

Native Node.js Solution: util.format(format[, ...args])

Example

  • Here is a more robust example demonstrating how to parameterize a literal String
import { format } from "util";

const rawURL = "mongodb+srv://%s:%s@myCluster.s73udhj.mongodb.net";
const username = "zappbrannigan";
const password = "VMJta9hdDGymmYMz";

const url = format(rawURL, username, password);

console.log(url);

// Output: "mongodb+srv://zappbrannigan:VMJta9hdDGymmYMz@myCluster.s73udhj.mongodb.net"

References