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 190 views
1 answer 190 views
1 answer 124 views
124 views asked Oct 12, 2024 by avibootz
1 answer 132 views
1 answer 194 views
194 views asked Jun 22, 2021 by avibootz
...