setprecision in output

G

Gary Wessle

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;
}
 
G

Gary Wessle

Gary Wessle said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top