![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Mar 2005
Location: different places. constantly on the run.
Posts: 57
Rep Power: 4
![]() |
um, rounding? anyone?
g'day, i'm currently studying through correspondence and one of the questions for an assignment has to do with changing pounds to kilo's, or yards to meters or something. that's fine, i can do that, but then it say round up to 2 decimals. i searched the site and quickly googled it, but didn't find anything that made sense. i looked in the books they gave me + the book i got from my mom's library, but that didn't help, because i couldn't find anything.
is there something in c++ that lets you set a standard "look" for your numbers like java's DecimalFormat? or do i have to stay up all night and drink lots of (strong) coffee and make my own function thingy that will look at the numbers after the "." and ceil() or floor() or something like that? oh, i'm very new new to c++. started studying this year, so you can't laugh at me and tell all your friends that there's actually a dude who doesn't know how to implement rounding in c++. thanks for the help(in advance of course)and for those who help me, you should pop round sometime and we can have a braai(bbq in english)and talk about integers, strings, etc. it'll be fun.Ü
__________________
There's got to be more to life than being really, really ridiculously good looking |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Feb 2005
Posts: 64
Rep Power: 4
![]() |
cout << fixed << setprecision(2) << value; |
|
|
|
|
|
#3 |
|
Programming Guru
![]() |
float round(float val, int prec) {
val *= (10^prec);
val = (int)val;
return val/(float)prec;
}
__________________
Last edited by tempest; Apr 5th, 2005 at 5:44 PM. |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Mar 2005
Location: different places. constantly on the run.
Posts: 57
Rep Power: 4
![]() |
here you go, this is what i've got. it works on some numbers, but not on all. i end up being out by 0.000000000000...00001 sometimes. sat for hours trying to work something out, but not getting there.
the Temp = ceil( f ); part works for the first part of the assignment's test data, but on the second i'm over by 0.000001, then it rounds up and then i'm over by 0.01 or something. stole the recursive function from another post here(thanks dude! ). if i take out the ceil( f ) bit, then on the first bit of data i'm under by 0.01 or so.i'm close to having a hernia. int exponent( int Num, int Exp )
{
if ( Exp == 0 )
return 1;
return ( Num * ( exponent( Num, Exp-1 ) ) );
}
float rounding( float f, int Exp )
{
int Temp;
int Div = exponent( 10, Exp );
f *= Div;
Temp = ceil( f );
f = (int)Temp;
f /= Div;
return f;
}thanks for the help(in advance of course).
__________________
There's got to be more to life than being really, really ridiculously good looking |
|
|
|
|
|
#5 |
|
Programmer
Join Date: Mar 2005
Location: different places. constantly on the run.
Posts: 57
Rep Power: 4
![]() |
when i use
cout << fixed << setprecision(2) << value; do i have to include something special, because it doesn't work when i compile. ![]()
__________________
There's got to be more to life than being really, really ridiculously good looking |
|
|
|
|
|
#6 |
|
Programmer
Join Date: Mar 2005
Location: Lexington, Ky
Posts: 58
Rep Power: 4
![]() |
Yes you do :
#include <iomanip> then you can use setprecision(n). n is the number of decimal places you want the program to output. |
|
|
|
|
|
#7 |
|
Programmer
Join Date: Mar 2005
Location: different places. constantly on the run.
Posts: 57
Rep Power: 4
![]() |
ok, so i included iomanip, but it still says "'setprecision' undeclared (first use this function)".
i'd rather ask a question and look stupid, than be stupid forever, so here goes... cout << fixed << setprecision(2) << value; what would it look like in my program? this: cout << fixed << setprecision(2) << Arr[i]; Arr[i] is an array of floats. i do feel slightly stupid now, but i'm sure it's not gonna last for long... and think of all the other people you're helping who are too affraid to ask and blah blah blah... thanks. ![]()
__________________
There's got to be more to life than being really, really ridiculously good looking |
|
|
|
|
|
#8 |
|
Programmer
Join Date: Feb 2005
Posts: 64
Rep Power: 4
![]() |
use namespace std , or fully qualify
#include <iostream>
#include <iomanip>
using namespace std; // if you don't do this you have to fully qualify cout, fixed, setprecision .....
int main () {
double f =3.14159;
cout << fixed << setprecision (2) << f << endl;
std::cout << std::fixed << std::setprecision (4) << f << std::endl;
return 0;
} |
|
|
|
|
|
#9 |
|
Programmer
Join Date: Mar 2005
Location: different places. constantly on the run.
Posts: 57
Rep Power: 4
![]() |
this is the whole thing so far. first two functions i spent hours on, but it doesn't work 100%.
//assignment 3 --> question 1a
#include <iostream>
#inclide <iomanip>
using namespace std;
int exponent( int Num, int Exp )
{
if ( Exp == 0 )
return 1;
return ( Num * ( exponent( Num, Exp-1 ) ) );
}
float rounding( float f, int Exp )
{
int Temp;
int Div = exponent( 10, Exp );
f *= Div;
//this worked for the first half of the test data, but second half it's out by a bit
// Temp = ceil( f );
f = (int)Temp;
f /= Div;
return f;
}
void getInput( float Arr[] )
{
for ( int i = 0 ; i < 20 ; i++ )
{
cin >> Arr[i];
}
}
void getDifference( float Arr1[], float Arr2[], float Arr3[] )
{
for ( int i = 0 ; i < 20 ; i++ )
{
Arr3[i] = Arr2[i] - Arr1[i];
}
}
void displayResults( string Message, float Arr[], int Round )
{
cout << "The differences in " << Message << " : " << endl << endl << endl;
for ( int i = 0 ; i < 20 ; i++ )
{
if ( Arr[i] < 0 )
{
// cout << i << " -> " << " down" << endl;
cout << fixed << setprecision(2) << -Arr[i];
}
else
{
if ( Arr[i] > 0 )
{
//this is my attempts at the rounding thing with those functions
cout << i << " -> " << rounding( Arr[i], Round ) << " up" << endl;
}
else
{
cout << i << " -> stayed the same" << endl;
}
}
}
cout << endl;
}
int main()
{
float Info2004[20], Info2005[20], Difference[20];
string Message;
cout << "Enter April 2004 lengths : " << endl;
getInput( Info2004 );
cout << "Enter April 2005 lengths : " << endl;
getInput( Info2005 );
getDifference( Info2004, Info2005, Difference );
Message = "weights";
displayResults( Message, Difference, 2 );
//*******************
cout << "Enter April 2004 weights : " << endl;
getInput( Info2004 );
cout << "Enter April 2005 weights : " << endl;
getInput( Info2005 );
getDifference( Info2004, Info2005, Difference );
Message = "lengths";
displayResults( Message, Difference, 1 );
system( "PAUSE" );
return 0;
}![]()
__________________
There's got to be more to life than being really, really ridiculously good looking |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|