New to C++: Decimals to Integers?

This is a discussion on New to C++: Decimals to Integers? within the C++ Programming forums, part of the General Programming Boards category; Hi everyone. I'm just learning C++. I know that you can write code that will convert a decimal number into ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    75

    Question New to C++: Decimals to Integers?

    Hi everyone. I'm just learning C++. I know that you can write code that will convert a decimal number into an integer, by just dropping the number after the decimal. But how would you write it if you want it to actually round the decimal off to the nearest integer... i.e.- instead of turning 7.9 into 7..... so that 7.9 turns into 8??? Hopefully that makes sense.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,261
    Look up the std::nint() [nearest integer] function in <cmath>.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    Quote Originally Posted by grumpy
    Look up the std::nint() [nearest integer] function in <cmath>.
    Forgive me, but where do I look that up? Is that something I look up on the compiler?

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,261
    <cmath> is described in the C++ standard (which means C++ compilers and libraries support it). The std::nint() function is declared in that header. Alternatively, you can use the <math.h> header from C (which C++ is backward compatible to) and the nint() function in that.

    If you have a help file with your compiler, you will usually find the standard library described in that. Otherwise, most basic texts will give you an introduction to functions in the standard library: so look up nint() in the index of such books.

    In this case, nint() is a function of the prototype "int nint(double);" or something equivalent for your compiler. The only difference is that the version in <cmath> is placed in namespace std, and the one in <math.h> is not. Those headers provide a number of basic mathematical functions.

    Just in case the following doesn't help, here's are three examples of usage (all equivalent, in terms of results).
    Code:
    //  The standard C++ way #1
    
    #include <cmath>
    
    int main()
    {
        double x = 2.7;
        int i = std::nint(x);    // i will have a value of 3
        return 0;
    }
    or
    Code:
    //  The standard C++ way #2
    
    #include <cmath>
    
    using namespace std;    //  Use this with caution;  there are some traps
    
    int main()
    {
        double x = 2.7;
        int i = nint(x);    // i will have a value of 3
        return 0;
    }
    or
    Code:
    /*  A standard C way.  C++ supports this, but it is often considered bad style in C++ */
    
    #include <math.h>
    
    int main()
    {
        double x = 2.7;
        int i = nint(x);    // i will have a value of 3
        return 0;
    }

  5. #5
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    simpler
    Code:
    #include <iostream>
    
    #define round_it(d) (static_cast<int>((d)+0.5))
    int main()
    {
        std::cout<<round_it(7.9)<<std::endl;
        std::cout<<round_it(2.34)<<std::endl;
        std::cout<<round_it(10.001)<<std::endl;
        return 0;
    }

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,261
    I disagree that your approach is "simpler". static_cast is a fairly advanced operation, technically. Using it for a simple mathematical operation is using a very big hammer to crack a very small nut.

    Using macros in C++ is often viewed as VERY bad style.

  7. #7
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Yuck Xerath.
    Write a function not a macro.
    Code:
    int round_it(int d)
    {
       return std::floor(d+0.5);
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    Code:
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    int main ()
    {
    	int sum;
    	int average;
    	double dec1;
    	double dec2;
    	double dec3;
    	double dec4;
    	double dec5;
    
    	cout << "Enter five decimal numbers: ";
    	cin >> dec1 >> dec2 >> dec3 >> dec4 >> dec5;
    	cout << endl;
    
    	cout << "The numbers you entered are: \n"
    	cout << dec1 << dec2 << dec3 << dec4 << dec5;
    	cout << endl;
    	
    	double a = dec1
    	double b = dec2
    	double c = dec3
    	double d = dec4
    	double e = dec5
    	int h = nint(a);
    	int i = nint(b);
    	int j = nint(c);
    	int k = nint(d);
    	int l = nint(e);
    
    	sum = h + i + j + k + l;
    	average = (h + i + j + k + l) / 5
    
    	cout << endl;
    	cout << "Sum: " << sum << endl;
    	cout << "Average: " << average << endl;
    
    	return 0;
    }


    ~~~ This is returning a lot of errors for me now. I hope you guys can see what this program is doing and can hopefully give me some advice.... there's probably a way to make it shorter, I just don't know how to do it! You guys have been a great help and I really appreciate it.

  9. #9
    *this
    Join Date
    Mar 2005
    Posts
    498
    since nint apparently doesnt exist in cmath why not just use the code mentioned to create it for yourself...

    Code:
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    int nint(double d)
    {
       return std::floor(d+0.5);
    }
    
    int main ()
    {
    	int sum;
    	int average;
    	double dec1;
    	double dec2;
    	double dec3;
    	double dec4;
    	double dec5;
    
    	cout << "Enter five decimal numbers: ";
    	cin >> dec1 >> dec2 >> dec3 >> dec4 >> dec5;
    	cout << endl;
    
    	cout << "The numbers you entered are: \n";   //forgot a semicolon
    	cout << dec1 << " " << dec2 << " " << dec3 << " " << dec4 << " " << dec5; //you probably want spaces between
    	cout << endl;
    	
    	double a = dec1;  //forgot a semicolon
    	double b = dec2;  //forgot a semicolon
    	double c = dec3;  //forgot a semicolon
    	double d = dec4;  //forgot a semicolon
    	double e = dec5;  //forgot a semicolon
    	//since there is no nint in cmath we use our own nint function
    	int h = nint(a);  
    	int i = nint(b); 
    	int j = nint(c); 
    	int k = nint(d);  
    	int l = nint(e);  
    
    	sum = h + i + j + k + l;
    	average = sum / 5;   //forgot a semicolon and why not just use the sum? and not do it again
    
    	cout << endl;
    	cout << "Sum: " << sum << endl;
    	cout << "Average: " << average << endl;
    
       cin.ignore(); //catch return
       cin.get(); //just to keep output open
       return 0;
    }
    Last edited by JoshR; 06-26-2005 at 12:48 PM.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    i don't understand why i need more semicolons. They're all there where they need to be, aren't they? I didn't think they went right after "cout" or "double" like that. And what is the "cin.ignore();" and "cin.get();" for?

  11. #11
    *this
    Join Date
    Mar 2005
    Posts
    498
    Well every statement in c++ ends with a semicolon, so basically your cout statements where skipping down untill the first semicolon, so the compiler detected a syntax error. Dont worry about cin.ignore() or cin.get() i just stuck them there so you could see the output even though you arent having trouble with it, it helped me see it so i didnt have to open up the command prompt.

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    Thank you so much! It works now!

  13. #13
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    why are macros 'bad style' ? enlighten me with something.

  14. #14
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Well for starters they have no respect for scope whatsoever. You should always prefer inline functions to macros.remember macros are expanded before the compiler even sees the code. behaviour is always predictable with inline functions whereas not so with macros.Read meyers effective c++ for more info or in fact just about any book on good coding stylein c++.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  15. #15
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by Stoned_Coder
    Well for starters they have no respect for scope whatsoever. You should always prefer inline functions to macros.remember macros are expanded before the compiler even sees the code. behaviour is always predictable with inline functions whereas not so with macros.Read meyers effective c++ for more info or in fact just about any book on good coding stylein c++.
    I agree, but that isn't a issue to an experienced programmer.
    But if you want to call it bad style, fine by me.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  2. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 01:24 PM
  3. Integers into array.
    By livestrng in forum C Programming
    Replies: 10
    Last Post: 10-29-2008, 11:35 PM
  4. Need help to show decimals
    By engstudent363 in forum C Programming
    Replies: 4
    Last Post: 02-19-2008, 03:13 PM
  5. Replies: 6
    Last Post: 08-04-2003, 10:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21