How to format any number to 2 decimal places in Node.js

2 Answers

0 votes
console.log(Number(5).toFixed(2)); 
console.log(Number(819).toFixed(2)); 
console.log(Number(3.14159).toFixed(2)); 
console.log(Number(187.789).toFixed(2)); 
 
    
    
    
    
/*
run:
    
5.00
819.00
3.14
187.79
    
*/

 



answered May 3, 2022 by avibootz
0 votes
console.log((Math.round(5 * 100) / 100).toFixed(2)); 
console.log((Math.round(819 * 100) / 100).toFixed(2)); 
console.log((Math.round(3.14159 * 100) / 100).toFixed(2)); 
console.log((Math.round(187.789 * 100) / 100).toFixed(2));  
    

    
    
/*
run:
    
5.00
819.00
3.14
187.79
    
*/

 



answered May 3, 2022 by avibootz

Related questions

2 answers 162 views
2 answers 162 views
1 answer 139 views
2 answers 145 views
2 answers 149 views
...