76

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

2
  • I would suggest looking at this SO thread as well. Commented Jul 2, 2011 at 22:54
  • I have since released a fast and spec compliant printf implementation for Node.js and browser github.com/gajus/fast-printf Commented Jan 3, 2021 at 2:53

6 Answers 6

84

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

Example:

util.format('hello %s', 'world'); // Returns: 'hello world' 
Sign up to request clarification or add additional context in comments.

8 Comments

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.
Similar to @Elliot Foster's comment, you could also do var printf = require('util').format;
@lapo floating point number can be rounded using toFixed() var a = 1.234567;a.toFixed(3) => >'1.235'
it's useless, it's not printf-like at all
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.
|
28

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

1 Comment

unfortunately sprintf-js is incompatible with colors: sprintf(" %s %s", title.grey, colors['blue'](msg)) ' \x1B[90mTitle\x1B[39m \x1B[34mMessage\x1B[39m'
7

Here is the javascript version of sprintf:

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

Comments

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 

4 Comments

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 ```
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.
useful tip (if it works) but it's equivalent to printf, not sprintf.
not sure why it doesn't work on node v9.4, just use `es6 template ${format}`
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

Comments

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:%[email protected]"; const username = "zappbrannigan"; const password = "VMJta9hdDGymmYMz"; const url = format(rawURL, username, password); console.log(url); // Output: "mongodb+srv://zappbrannigan:[email protected]" 

References

1 Comment

it's so hopeless! you can't even do %10s etc.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.