How to format any number to 2 decimal places in JavaScript

2 Answers

0 votes
console.log(Number(3).toFixed(2)); 
console.log(Number(982).toFixed(2)); 
console.log(Number(3.14159).toFixed(2)); 
console.log(Number(182.789).toFixed(2)); 

   
   
   
   
/*
run:
   
"3.00"
"982.00"
"3.14"
"182.79"
   
*/

 



answered May 2, 2022 by avibootz
edited May 2, 2022 by avibootz
0 votes
console.log((Math.round(3 * 100) / 100).toFixed(2)); 
console.log((Math.round(982 * 100) / 100).toFixed(2)); 
console.log((Math.round(3.14159 * 100) / 100).toFixed(2)); 
console.log((Math.round(182.789 * 100) / 100).toFixed(2)); 

  
  
  
  
/*
run:
  
"3.00"
"982.00"
"3.14"
"182.79"
  
*/

 



answered May 2, 2022 by avibootz

Related questions

2 answers 164 views
2 answers 162 views
1 answer 190 views
1 answer 165 views
...