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

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

java.util.scanner - How to read and add only numbers to array from a text file -

iphone - Three second countdown in cocos2d -