How to format number to always show 2 decimal places in JavaScript

4 Answers

0 votes
const num = 1

console.log(num.toFixed(2));
 
 
 
   
     
 
/*
run:
     
"1.00"
     
*/

 



answered Aug 4, 2021 by avibootz
0 votes
const num = 1.3489

console.log(num.toFixed(2));
 
 
 
   
     
 
/*
run:
     
"1.35"
     
*/

 



answered Aug 4, 2021 by avibootz
0 votes
const num = 1.3452

console.log(num.toFixed(2));
 
 
 
   
     
 
/*
run:
     
"1.35"
     
*/

 



answered Aug 4, 2021 by avibootz
0 votes
const num = 1.3441

console.log(num.toFixed(2));
 
 
 
   
     
 
/*
run:
     
"1.34"
     
*/

 



answered Aug 4, 2021 by avibootz

Related questions

2 answers 152 views
1 answer 174 views
2 answers 159 views
2 answers 156 views
1 answer 125 views
1 answer 158 views
...