How to round a float number down and up to 2 decimal places in C++

1 Answer

0 votes
#include <iostream>
#include <cmath>

int main() {
    float f = 376.8713f;
 
    float round_down = floorf(f * 100) / 100;   
    float round_up = ceilf(f * 100) / 100; 
      
    std::cout << f << "\n";
    std::cout << round_down << "\n";
    std::cout << round_up << "\n";
}

 
 
/*
run:
 
376.871
376.87
376.88
 
*/

 



answered Sep 4, 2019 by avibootz
edited May 15, 2025 by avibootz

Related questions

1 answer 265 views
2 answers 83 views
2 answers 77 views
2 answers 81 views
1 answer 147 views
3 answers 239 views
1 answer 162 views
...