To print formatted console output with Node.js, we use the sprintf-js
module.
For instance, we write
const { sprintf } = require("sprintf-js");
console.log(sprintf("Space Padded => %10.2f", 123.4567));
to call sprintf
with the format string and the value we want to print.
We round the number to 2 decimal places with %10.2f
and return the formatted string which we print with console.log
.