### Duplicate of: round() for float in C++

I'm using VS2008 and I've included math.h but I still can't find a round function. Does it exist?

I'm seeing a bunch of "add 0.5 and cast to int" solutions on google. Is that the best practice?

Skip to content
# C++ – Where is Round() in C++?

### Duplicate of: round() for float in C++

###### Related Question

c++rounding

I'm using VS2008 and I've included math.h but I still can't find a round function. Does it exist?

I'm seeing a bunch of "add 0.5 and cast to int" solutions on google. Is that the best practice?

- C++ – Where and why do I have to put the “template” and “typename” keywords
- Python – How to round UP a number
- Javascript – How to round to 1 decimal place in Javascript
- Javascript – How to round float numbers in javascript
- Javascript – How to round to at most 2 decimal places, if necessary
- C++ – Avoiding if statement inside a for loop
- Python – How to round to 2 decimals with Python

## Best Solution

You may use C++11's

`std::round()`

.If you are still stuck with older standards, you may use

`std::floor()`

, which always rounds to the lower number, and`std::ceil()`

, which always rounds to the higher number.To get the normal rounding behaviour, you would indeed use

`floor(i + 0.5)`

.This way will give you problems with negative numbers, a workaround for that problem is by using ceil() for negative numbers:

Another, cleaner, but more resource-intensive, way is to make use of a stringstream and the input-/output-manipulators:

Only use the second approach if you are not low on resources and/or need to have control over the precision.