Node: util.format not printing % when there's multiple arguments

Created on 14 Jun 2017  路  7Comments  路  Source: nodejs/node

  • v8.1.0:
  • Darwin MacBook-Pro.local 16.6.0 Darwin Kernel Version 16.6.0: Fri Apr 14 16:21:16 PDT 2017; root:xnu-3789.60.24~6/RELEASE_X86_64 x86_64:
  • Subsystem:


The following code in node 6.10.3 prints percent: 10%, fraction: 0.1

console.log('percent: %d%, fraction: %d', 10, 0.1);

In node 8.1.0 it prints percent: 10 fraction: 0.1. Notice the % is no longer printed.

The following code gets back to the original node 6.10.3 output

console.log('percent: %d%%, fraction: %d', 10, 0.1);

Note the %% to print a single % in node 8

Also worth noting this problem doesn't happen if you are just printing one variable in node 8. The following code prints percent: 10%

console.log('percent: %d%', 10);

Should node 8 be formatting the same results as node 6? Or is this a functional change?

util

Most helpful comment

Ideally we should throw an error in such cases. The correct format string here is 'percent: %d%%, fraction: %d'.

All 7 comments

/cc @nodejs/ctc

/cc @jseijas

Ideally we should throw an error in such cases. The correct format string here is 'percent: %d%%, fraction: %d'.

Yes, a throw would be ideal, but from my tests browsers don't throw on it either, so It'd be better to not throw for compat. I tested the case console.log('percent: %d%, fraction: %d', 10, 0.1); in a few browsers:

Firefox: percent: 10%, fraction: 0
Chrome: percent: 10%, fraction: 0
Safari: percent: 10[object Object] fraction: %6d

Quite an interesting result in Safari (also present in WebKit Nightly).

I can confirm the correct format of console.log('percent: %d%%, fraction: %d', 10, 0.1); gives the desired output of percent: 10%, fraction: 0.1 in Node 6.10.3 and 0.10.48

Was this page helpful?
0 / 5 - 0 ratings