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 250 views
2 answers 68 views
2 answers 65 views
2 answers 58 views
1 answer 122 views
3 answers 194 views
1 answer 145 views
...