Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a sprintf equivalent for node.js

Tags:

node.js

printf

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

like image 513
Steven Mohapi-Banks Avatar asked Apr 23 '10 10:04

Steven Mohapi-Banks


People also ask

What is Sprintf in node JS?

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.

How do you Sprintf in JavaScript?

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).

Is there an alternative to node js?

AngularJS, PHP, Python, JavaScript, and React are the most popular alternatives and competitors to Node. js.

Is there an F string in JavaScript?

In javascript, there is no built-in string formatting function.


2 Answers

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

Example:

util.format('hello %s', 'world'); // Returns: 'hello world' 
like image 56
lapo Avatar answered Sep 19 '22 15:09

lapo


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

  • sprintf (deprecated now)
  • sprintf-js
  • fast-printf
like image 42
KARASZI István Avatar answered Sep 18 '22 15:09

KARASZI István