Connecting Tech Pros Worldwide Forums | Help | Site Map

setprecision in output

Gary Wessle
Guest
 
Posts: n/a
#1: Aug 8 '06
hi

the code below is giving me what I want but it is very ugly. and will
not work for a long list of different length numbers.
could you please look at it and comment.

thank you

the desired output is:
0.555504 of an inch foreach 12.55


#include <iostream>
using std::cout;
using std::endl;
#include <iomanip>
using std::setw;
using std::left;
using std::setprecision;
#include <cmath>
using std::log10;

int main(){
double a = 0.5555040000;
double b = 12.5500;
int aa = 1000000;
int bb = 10000;
int za = static_cast<int(log10(aa));
int zb = static_cast<int(log10(bb));
cout << setw(10) << setprecision(za) << left << a << "of an inch foreach "
<< setw(10) << setprecision(zb) << b << endl;
}


Gary Wessle
Guest
 
Posts: n/a
#2: Aug 8 '06

re: setprecision in output


Gary Wessle <phddas@yahoo.comwrites:
Quote:
hi
>
the code below is giving me what I want but it is very ugly. and will
not work for a long list of different length numbers.
could you please look at it and comment.
>
thank you
>
the desired output is:
0.555504 of an inch foreach 12.55
>
>
#include <iostream>
using std::cout;
using std::endl;
#include <iomanip>
using std::setw;
using std::left;
using std::setprecision;
#include <cmath>
using std::log10;
>
int main(){
double a = 0.5555040000;
double b = 12.5500;
int aa = 1000000;
int bb = 10000;
int za = static_cast<int(log10(aa));
int zb = static_cast<int(log10(bb));
cout << setw(10) << setprecision(za) << left << a << "of an inch foreach "
<< setw(10) << setprecision(zb) << b << endl;
}
in addition,
given double x = 2, I need to add x to a and b such
0.555504 + .0000002 = 0.555506
12.55 + .02 = 12.57
so that the output of the cout would be
0.555506 of an inch foreach 12.57
Closed Thread