Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 134923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:42:04+00:00 2026-05-11T06:42:04+00:00

Duplicate of: round() for float in C++ I’m using VS2008 and I’ve included math.h

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T06:42:05+00:00Added an answer on May 11, 2026 at 6:42 am

    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:

    double round(double number) {     return number < 0.0 ? ceil(number - 0.5) : floor(number + 0.5); }

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

    #include <iostream> #include <sstream>  double round(double val, int precision) {     std::stringstream s;     s << std::setprecision(precision) << std::setiosflags(std::ios_base::fixed) << val;     s >> val;     return val; }

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: .Net Round Bug In C#: Math.Round(2.5) result is 2 (instead of 3)!
Possible Duplicate: Rounding numbers in Objective-C In Objective-c How to round off Float values?
Possible Duplicate: Double value to round up in Java I am getting float number
Possible Duplicate: PHP Round function - round up to 2 dp? What my problem
Possible Duplicate: How can I round of to whole numbers in JavaScript? In javascript
Possible Duplicate: Can anybody suggest the best image resize script in php? I'm still
Possible Duplicate: c# - How do I round a decimal value to 2 decimal
Duplicate Of : Find node clicked under context menu I've got a context menu
Possible Duplicate: The accuracy of PHP float calculate <?php $foo = 0; do {
Possible Duplicate: c# - How do I round a decimal value to 2 decimal

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.