Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rounding Decimal Places 1

Status
Not open for further replies.

cmackowski

IS-IT--Management
Jun 23, 2000
20
US
Hello,

I'm taking an intro to c++ and having problems with one of my projects. As part of it, I need to round to the second decimal place. I have searched all over my books and plenty of time on google before this. I even emailed the instructor who has not responded. So I need to take a number like 12.342 and round to 12.34.
Any Help would be greatly appreciated!!

-Chris
 
Code:
double round2(double x)
{
  double iport, fport;
	
  fport = modf(x,&iport);
  return fport != 0.0?iport + floor(fport*100+0.5)/100.0:x;
}
 
Oh, I'm sorry. Don't forget: all decimal fractions (0.01, for example;) are approximated values on binary computers. Add 0.01 1000000 times - result is not exactly 10000...
 
This would round to the second decimal place correct? 12.3456789 would still round to 12.35?

Thanks for your Help!!
 
Also in your example, where would I call the input from my variable?
 
12.3456789? Try (and see;) or better try to understand the method...
Sorry, but I can't understand your last question. You have now the function to round. It's 100% working example to round any float point numbers to the 2nd decimal place (with common rules), so use it... What else?
Apropos, if you want only to print rounded value: all format conversions make the rounding...
 
Could someone point out what is wrong here. I can't seem to input the variable into this function.....

#include "iostream" /* include stdio.h header file */
#include "cmath" /* include math.h header file */

using namespace std;

/* Function Prototypes */
float round2(float r);

int main () //start of main function

{
float round2,round,Value;

cout << "Enter Value: "; cin >> Value;
round = round2;


return 0; // Exit main function

}




//float round2(float r)
//double round2(double x)
//{
//floor( Value*100+0.5 )/100.0;
//}

double round2(double x)
{
double iport, fport;
Value = modf(x,&iport);
return fport != 0.0?iport +
floor(fport*100+0.5)/100.0:x;
}


Thanks
 
Oh my!...
Code:
double round2(double); // not float, see round2 definition.

double value, rounded;

cout << "Enter value: ";
cin >> value;
rounded = round2(value);    
cout << rounded << endl;
...
 
OOh my is right........Thanks for all your help.
Still having problems though..

#include "iostream" /* include stdio.h header file */
#include "cmath" /* include math.h header file */

using namespace std;

double round2(double); // not float, see round2 definition.

double value, rounded;

cout << "Enter value: "; cin >> value;
rounded = round2(value);
cout << rounded << endl;
return 0; // Exit main function

}

double round2(double x)
{
double iport, fport;
fport = modf(x,&iport);
return fport != 0.0?iport +
floor(fport*100+0.5)/100.0:x;
}

I get the following errors:

c:\docume~1\x0005432\desktop\lab2-1.cpp:50: syntax error before `<'
c:\docume~1\x0005432\desktop\lab2-1.cpp:50: syntax error before `>'
c:\docume~1\x0005432\desktop\lab2-1.cpp:51: ANSI C++ forbids declaration `rounded' with no type
c:\docume~1\x0005432\desktop\lab2-1.cpp:51: conflicting types for `int rounded'
c:\docume~1\x0005432\desktop\lab2-1.cpp:48: previous declaration as `double rounded'
c:\docume~1\x0005432\desktop\lab2-1.cpp:51: warning: initialization to `int' from `double'
c:\docume~1\x0005432\desktop\lab2-1.cpp:52: syntax error before `<'

 
Code:
#include <iostream>
#include <cmath>
...
Use angle brackets (<...>), not quotes for system headers.
Quotes for your header files only.
 
Well, try this:
Code:
... includes (with <>)
using namespace std;
... round2() definition
int main()
{
  double x;

  while (cout << "Enter number (or what else to quit): ",
         cin >> x)
     cout << "round2(" << x << ") == " << round2(x) << endl;
  return 0;
}
 
I changed to the brackets (I found I needed the quotes using vi and gcc)

Still getting these errors:

c:\docume~1\x0005432\desktop\lab2-1.cpp:51: syntax error before `<'
c:\docume~1\x0005432\desktop\lab2-1.cpp:51: syntax error before `>'
c:\docume~1\x0005432\desktop\lab2-1.cpp:52: ANSI C++ forbids declaration `rounded' with no type
c:\docume~1\x0005432\desktop\lab2-1.cpp:52: conflicting types for `int rounded'
c:\docume~1\x0005432\desktop\lab2-1.cpp:48: previous declaration as `double rounded'
c:\docume~1\x0005432\desktop\lab2-1.cpp:52: warning: initialization to `int' from `double'
c:\docume~1\x0005432\desktop\lab2-1.cpp:53: syntax error before `<'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top