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
Sprintf is a JavaScript library. This is an independent library that is used for making print statements happen. So, the idea behind sprint is to execute print statements onto the console. The sprintf makes these print statements in a very sophisticated manner, the node.
The equivalent of sprintf("%. 2f", num) in JavaScript seems to be num. toFixed(2) , which formats num to 2 decimal places, with rounding (but see @ars265's comment about Math. round below).
AngularJS, PHP, Python, JavaScript, and React are the most popular alternatives and competitors to Node. js.
In javascript, there is no built-in string formatting function.
There is now printf
-like support in util.format()
.
Example:
util.format('hello %s', 'world'); // Returns: 'hello world'
There are couple in the npm registry which are actual sprintf
implementations since util.format
has just a very basic support.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With