How to convert float to int in C++

2 Answers

0 votes
#include <iostream>

int main() {
    float f = 3.14;
    int i = (int)f;
        
    std::cout << i;
    
    return 0;
}




/*
run:

3

*/

 



answered May 17, 2021 by avibootz
0 votes
#include <iostream>

int main() {
    float f = 3.14;
    int i = static_cast<int>(f);
        
    std::cout << i;
    
    return 0;
}




/*
run:

3

*/

 



answered May 17, 2021 by avibootz

Related questions

2 answers 146 views
1 answer 147 views
1 answer 105 views
5 answers 377 views
377 views asked Jun 20, 2021 by avibootz
2 answers 215 views
215 views asked May 16, 2021 by avibootz
...