c++ - How to +1 to the digit if the first decimal is greater than or equal to 5? -
my goal add value in front of decimal place when first decimal places more or equal 5.
for example:
#include <iostream> using namespace std; int main() { float num = 0.5222f; cout << (int)num << endl; cin.get(); return 0; } my intended result 1 instead of 0. how should modify code expected result?
if want round value nearest integer, add 0.5 before casting int:
float num = 0.5222f; cout << (int)(num + 0.5); or alternatively might use 1 of following functions <cmath> header:
double round (double x); float roundf (float x); long double roundl (long double x);
Comments
Post a Comment