How to convert a float number to a whole number in JavaScript

4 Answers

0 votes
let f = 3.5;
 
console.log(parseInt(f));
console.log(Math.round(f)); 
console.log(Math.floor(f)); 
console.log(Math.ceil(f));
console.log(Math.trunc(f));
 
  
    
/*
run:
         
3
4
3
4
3
        
*/
         

 

 



answered Mar 28, 2019 by avibootz
edited May 18, 2024 by avibootz
0 votes
let f = -3.5;
 
console.log(parseInt(f));
console.log(Math.round(f)); 
console.log(Math.floor(f)); 
console.log(Math.ceil(f));
console.log(Math.trunc(f));
 
  
    
/*
run:
         
-3
-3
-4
-3
-3
        
*/

 



answered Mar 28, 2019 by avibootz
edited May 18, 2024 by avibootz
0 votes
console.log(parseInt(2.4));
console.log(Math.round(2.4)); 
console.log(Math.floor(2.4)); 
console.log(Math.ceil(2.4));
console.log(Math.trunc(2.4));
 
  
    
/*
run:
         
2
2
2
3
2

*/

 



answered May 18, 2024 by avibootz
0 votes
console.log(parseInt(2.6));
console.log(Math.round(2.6)); 
console.log(Math.floor(2.6)); 
console.log(Math.ceil(2.6));
console.log(Math.trunc(2.6));
 
  
    
/*
run:
         
2
3
2
3
2

*/

 



answered May 18, 2024 by avibootz

Related questions

1 answer 136 views
2 answers 167 views
1 answer 190 views
...