Time and Numeric Utilities
Round Values
Rounding helpers turn fractional values into nearby whole-number choices.
Round Values
round_values.cpp
#include <cmath>
#include <iostream>
int main() {
double raw = ;
int down = static_cast<int>(std::floor(raw));
int up = static_cast<int>(std::ceil(raw));
int nearest = static_cast<int>(std::round(raw));
std::cout << "raw=" << raw << std::endl;
std::cout << "down=" << down << std::endl;
std::cout << "up=" << up << std::endl;
std::cout << "nearest=" << nearest << std::endl;
return 0;
}
#include <cmath>
#include <iostream>
int main() {
double raw = ;
int down = static_cast<int>(std::floor(raw));
int up = static_cast<int>(std::ceil(raw));
int nearest = static_cast<int>(std::round(raw));
std::cout << "raw=" << raw << std::endl;
std::cout << "down=" << down << std::endl;
std::cout << "up=" << up << std::endl;
std::cout << "nearest=" << nearest << std::endl;
return 0;
}
#include <cmath>
#include <iostream>
int main() {
double raw = ;
int down = static_cast<int>(std::floor(raw));
int up = static_cast<int>(std::ceil(raw));
int nearest = static_cast<int>(std::round(raw));
std::cout << "raw=" << raw << std::endl;
std::cout << "down=" << down << std::endl;
std::cout << "up=" << up << std::endl;
std::cout << "nearest=" << nearest << std::endl;
return 0;
}
floor
`std::floor` moves to the greatest whole number not greater than the value.
ceil
`std::ceil` moves to the smallest whole number not less than the value.
round
`std::round` moves to the nearest whole number.