How to format any number to 2 decimal places in TypeScript

2 Answers

0 votes
console.log(Number(2).toFixed(2)); 
console.log(Number(782).toFixed(2)); 
console.log(Number(3.14159).toFixed(2)); 
console.log(Number(183.789).toFixed(2)); 
 
    
    
    
    
/*
run:
    
"2.00" 
"782.00" 
"3.14" 
"183.79" 
    
*/

 



answered May 3, 2022 by avibootz
0 votes
console.log((Math.round(2 * 100) / 100).toFixed(2)); 
console.log((Math.round(782 * 100) / 100).toFixed(2)); 
console.log((Math.round(3.14159 * 100) / 100).toFixed(2)); 
console.log((Math.round(183.789 * 100) / 100).toFixed(2)); 
 
    
    
    
    
/*
run:
    
"2.00" 
"782.00" 
"3.14" 
"183.79" 
    
*/

 



answered May 3, 2022 by avibootz

Related questions

2 answers 136 views
2 answers 122 views
2 answers 113 views
...