Thread: round

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    32

    round

    hi everyone

    I would like to roungd 2.34567 to 2.34, can I have something like that in C++.

    if you see this
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main ()
    {
      printf ("floor of 2.3 is %.1lf\n", floor (2.3) );
      printf ("floor of 3.8 is %.1lf\n", floor (3.8) );
      printf ("floor of -2.3 is %.1lf\n", floor (-2.3) );
      printf ("floor of -3.8 is %.1lf\n", floor (-3.8) );
      return 0;
    }
    the output is 2.0
    3.0
    -2.3
    -3.8

    I do want something like this, I would like to reduce the numbers after the point.

    so this number 2.456633 would lke it to be 2.46

    What can I use, what math operator.

    thank you inadvance

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Quote Originally Posted by yes
    hi everyone

    I would like to roungd 2.34567 to 2.34, can I have something like that in C++.
    sure you don't mean round up to 2.35?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main ()
    {  
    	double f = 3.14159;
    	cout.precision(2);
    	cout<< f << endl;
    
    	return 0;
    }

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    125
    If X is your variable, just do it like this:

    X=round(X*10)/10;

    Will round X to the first decimal.

    I recommend you use std::cout instead of printf by the way. Streams also have an option to change the precision of their output of floating point numbers, which is much easier than rounding everything by hand. (in other words, do what twomers told you to)
    Typing stuff in Code::Blocks 8.02, compiling stuff with MinGW 3.4.5.

  4. #4
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    Do you want to store the rounded number in a variable, or just output the rounded version and keep the variable as is?

    [edit] beaten...twice! damn slow connection... [/edit]
    There is a difference between tedious and difficult.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    32
    I would like just to round my Avg in my output file.

    The Avg gets it value after a calculation and print its out put in a stream file. so now How do I round the Avg only.if my output statement like below;

    Code:
    outfile << Avg << Min << Max << endl;
    thank you

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    If you want rounding for output only try this
    Code:
    outfile << fixed << setprecision(2) << Avg << Min << Max << endl;
    you need to #include <iomanip>
    Kurt

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    32
    Thank you so much ZUK it is the perfect thing to my outfile.

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    14
    Quote Originally Posted by ZuK
    If you want rounding for output only try this
    Code:
    outfile << fixed << setprecision(2) << Avg << Min << Max << endl;
    you need to #include <iomanip>
    Kurt
    I was looking for this answer.. Thanks for very useful information.. I love this board..

  9. #9
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Thread necrophilia... ewwwwwwww.

  10. #10
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Closed.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 11-03-2008, 09:48 PM
  2. Fibonacci Formula, How to round in C++?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 10-15-2004, 10:47 AM
  3. I need some quick help with code.
    By stehigs321 in forum C Programming
    Replies: 35
    Last Post: 10-30-2003, 10:07 PM
  4. Someone help me with this game??
    By stehigs321 in forum Game Programming
    Replies: 15
    Last Post: 10-30-2003, 09:42 PM
  5. Please help with some coding..
    By stehigs321 in forum C Programming
    Replies: 2
    Last Post: 10-27-2003, 06:44 PM