Thread: C++) How would I display a value rounded to the nearest integer?

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    45

    C++) How would I display a value rounded to the nearest integer?

    bump....
    Last edited by xbusterx; 09-21-2008 at 04:05 PM.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    First you need to read the number with decimal places, so read in a float, not an int.

    Then you can either just use std::setprecision to make the output operator do the rounding, or you can use a cast to int to cut away the decimal places. Since the latter always rounds down, though, you can add .5 first:
    Code:
    float f1 = 3.2f;
    int i1 = static_cast<int>(f1 + 0.5f); // -> 3.7 -> 3
    float f2 = 3.7f;
    int i2 = static_cast<int>(f2 + 0.5f); // -> 4.2 -> 4
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User Will Hemsworth's Avatar
    Join Date
    Sep 2008
    Location
    England - Lymm
    Posts
    6
    If your going to manually round it, CornedBee's suggestion will only work for positive numbers,
    but incase you need to do it with a negative value, you can use this simple function.
    Code:
    #define Abs(val)  ((val) < 0 ? -(val) : (val))
    int round(float f) {
       int abs_rounded = static_cast<int>( Abs(f) + 0.5f );
       return (f < 0.0f ? -abs_rounded : abs_rounded);
    }
    Hope this helps.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    45
    Ok here is the new one




    float num;

    cout<< "Please enter a positive value";

    cin>> num=static_cast<int>(num + 0.5f);

    cout<< num << endl;

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    cin>> num=static_cast<int>(num + 0.5f);
    What exactly do you expect this line to do?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    45
    Quote Originally Posted by CornedBee View Post
    What exactly do you expect this line to do?
    Well the user will display a number and 0.5 is added to that number.

    so if it's 3.1 it becomes 3.6, but since it's a interger it will round down to a 3.

    That line is just to round the number and put it on the bottom line where is says:


    cout<< num << endl;

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by xbusterx View Post
    Well the user will display a number and 0.5 is added to that number.

    so if it's 3.1 it becomes 3.6, but since it's a interger it will round down to a 3.

    That line is just to round the number and put it on the bottom line where is says:


    cout<< num << endl;
    Yes, but you need to read the number first, then do the math, not all in one line.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by xbusterx View Post
    cin>> num=static_cast<int>(num + 0.5f);
    There are a few problems with this line:
    1)cin>> num evaluates to cin. This is so tht you can chain multiple >> together. But in this case that means you assign the value on the right to cin, not num.
    2) you use num and change num in the same expression. Using a variable in this way is disallowed, except when the old variable is used to calculate it's new value, or when sequencing operators (&&, ||, ?:, and ,[comma]) seperate the two uses.
    3)You also intended to change num twice in the same line. this is also disallowed, except again when sequencing operators are used.

    As matsp said, break this expression into parts.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No Match For Operator+ ???????
    By Paul22000 in forum C++ Programming
    Replies: 24
    Last Post: 05-14-2008, 10:53 AM
  2. question in rounding integer in c
    By DrGreat in forum C Programming
    Replies: 5
    Last Post: 04-13-2008, 08:13 AM
  3. How to read in an integer and display it again
    By axr0284 in forum C++ Programming
    Replies: 7
    Last Post: 12-07-2004, 01:37 PM
  4. newbie programmer - needs help bad.
    By hortonheat in forum C Programming
    Replies: 17
    Last Post: 10-20-2004, 05:31 PM
  5. help with a source code..
    By venom424 in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2004, 12:42 PM