# New to C++: Decimals to Integers?

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 06-25-2005
MyntiFresh
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.
• 06-25-2005
grumpy
Look up the std::nint() [nearest integer] function in <cmath>.
• 06-25-2005
MyntiFresh
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?
• 06-26-2005
grumpy
<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; }```
• 06-26-2005
xErath
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; }```
• 06-26-2005
grumpy
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.
• 06-26-2005
Stoned_Coder
Yuck Xerath.
Write a function not a macro.
Code:

```int round_it(int d) {   return std::floor(d+0.5); }```
• 06-26-2005
MyntiFresh
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.
• 06-26-2005
JoshR
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; }```
• 06-26-2005
MyntiFresh
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?
• 06-26-2005
JoshR
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.
• 06-26-2005
MyntiFresh
Thank you so much! It works now!
• 06-26-2005
xErath
why are macros 'bad style' ? enlighten me with something.
• 06-26-2005
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++.
• 06-26-2005
xErath
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.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last