Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the JavaScript equivalent of PHP's sprintf function? [duplicate]

Here is my PHP code:

$kg= 75.20283619791667;
$lbs = sprintf("%.1f",$kg*2.20462);
// $lbs should equal 165.8

What is the equivalent in JavaScript if I start with:

var kg=75.20283619791667;
var lbs=2.20462*kg;

And I want to end with 165.8 as in the PHP example.

like image 634
somejkuser Avatar asked Nov 29 '25 05:11

somejkuser


1 Answers

There is no out of the box equivalent for sprintf in javascript. But there are already some libraries which implemented this function on their own. For example you could have a look at: https://github.com/alexei/sprintf.js

like image 176
Philipp Avatar answered Dec 01 '25 19:12

Philipp