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 272 views
1 answer 287 views
2 answers 298 views
2 answers 262 views
1 answer 219 views
1 answer 269 views
...