CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2002
    Posts
    55

    round to integer

    I can´t find any function for doing this.Is there any fast,simple way of doing it?

    Thank you.

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125
    For positive numbers it is simple:

    Code:
    double y = 1.234;
    int x = reinterpret_cast<int>(y + 0.5);
    For negative numbers you have to decide if you are rounding DOWN (a more negative number) or TOWARDS zero. That will change the sign....
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  3. #3
    Join Date
    May 2001
    Location
    Madrid-Spain
    Posts
    1,123
    I don't know if there is any function, but an easy way to do it is this:

    Code:
    double number = ...;
    
    int round = (number<0) ? (int)(number-0.5) : (int)(number+0.5);
    I hope this helps you.
    Last edited by irona20; January 8th, 2003 at 11:22 AM.
    I am Miss Maiden... Miss Iron Maiden :-D

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured