add 0.5 before casting (if x > 0) or subtract 0.5 (if x < 0), because the compiler will always truncate.
float x = 55; // stored as 54.999999... x = x + 0.5 - (x<0); // x is now 55.499999... int y = (int)x; // truncated to 55
C++11 also introduces std::round, which likely uses a similar logic of adding 0.5 to |x| under the hood (see the link if interested) but is obviously more robust.
A follow up question might be why the float isn’t stored as exactly 55. For an explanation, see this stackoverflow answer
Related Posts:
- How do you round UP a number?
- How do you round UP a number?
- What is the difference between float and double?
- Floating Point Exception C++ Why and what is it?
- Round a floating-point number down to the nearest integer?
- Limiting floats to two decimal points
- 1e-9 or -1e9, which one is correct?
- C: printf a float value
- C++ round a double up to 2 decimal places
- Floating Point Exception C++ Why and what is it?
- Random float number generation
- Why does a float variable stop incrementing at 16777216 in C#?
- C++ int float casting
- How to convert float number to Binary?
- Difference between decimal, float and double in .NET?
- Difference between decimal, float and double in .NET?
- Float and double datatype in Java
- C++ floating point to integer type conversions
- C++ float array initialization
- What is a segmentation fault?
- Why in C++ do we use DWORD rather than unsigned int? [duplicate]
- C++ std::priority_queue uses the lambda expression
- When to use reinterpret_cast?
- Expression must be a modifiable lvalue
- How to read a file line by line or a whole text file at once?
- How to print elements in a vector c++
- Split a string using C++11
- error: expected unqualified-id error: Meaning and fix? [duplicate]
- How to track down a “double free or corruption” error
- The Definitive C++ Book Guide and List
- C++ code file extension? What is the difference between .cc and .cpp
- undefined reference to `WinMain@16′
- How to round to 2 decimals with Python?
- What is C# equivalent of
- “X does not name a type” error in C++
- TranslateName() function doesn’t work on win10
- How to use the PI constant in C++
- How to concatenate two strings in C++?
- How to get current time and date in C++?
- where is the official c++ documentation
- wntdll.pdb not loaded – Can’t see the exception
- Setting an int to Infinity in C++
- dynamic_cast and static_cast in C++
- What exactly is std::atomic?
- Separating class code into a header and cpp file
- Converting a vector
to string - std::string formatting like sprintf
- Sorting Characters Of A C++ String
- Sentinel while loop for C++
- How to ensure that a std::map is ordered?
- How to write C++ getters and setters
- std::out_of_range error?
- libpng warning: iCCP: known incorrect sRGB profile
- C++: Expression must have a constant value when declaring array inside function
- What does ‘const static’ mean in C and C++?
- How to determine if a string is a number with C++?
- C++ wait for user input
- non-standard syntax; use ‘&’ to create a pointer to member
- How to shuffle a std::vector?
- Center text in fixed-width field with stream manipulators in C++
- ctypes error: libdc1394 error: Failed to initialize libdc1394
- Understanding glm::lookAt()
- How to get current timestamp in milliseconds since 1970 just the way Java gets
- What is the best open XML parser for C++?
- Debug vs Release in CMake
- C++ Matrix Class
- access violation reading location c++
- C++ template constructor
- PlaySound in C++ Console application?
- Creation of Dynamic Array of Dynamic Objects in C++
- Compiling simple Hello World program on OS X via command line
- Cannot open output file, permission denied
- Strange error C2131: expression did not evaluate to a constant in VC 2015
- c++ reading csv file
- Why do you use typedef when declaring an enum in C++?
- When to use const char * and when to use const char []
- What is wrong with using goto?
- Double pointer array in c++
- Convert floats to ints in Pandas?
- Convert double to Int, rounded down
- How well is Unicode supported in C++11?
- How to convert string to float?
- The CXX compiler identification is unknown
- Is there a standard sign function (signum, sgn) in C/C++?
- C++ compiler error: “return type specification for constructor invalid”
- error: ISO C++ forbids in-class initialization of non-const static member
- Function call missing argument list to create pointer
- cannot specify explicit initializer for arrays
- What does ** mean in C++?
- Incomplete type is not allowed: stringstream
- Make / gcc cryptic error 2: how to have more information?
- REGSVR32: the module “xxxxx.dll” failed to load … dependent assembly could not be found
- Error: expression cannot be used as a function?
- What is a .h.gch file?
- MinGW .exe requires a few gcc dll’s regardless of the code?
- Converting bool to text in C++
- Convert float to double without losing precision
- clearing a vector of pointers [duplicate]
- Evaluate a string with a switch in C++ [duplicate]