Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,884 questions

51,810 answers

573 users

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 123 views
2 answers 155 views
1 answer 161 views
...