Needing to align floats by the first digit, not by the decimal. I'm not 100% on the intricacies of setw(), so not sure if the output I'm looking for is even possible. Tried searching for an answer for a couple hours now, and nothing seems to be exactly what I'm looking for. The field width isn't accurate here, they're just placements, but they still don't give desired output regardless.
Current code:
cout << setw(14) << collections1 << setw(18) << collections2 << setw(15) <<
collections3 << endl ;
Current output:
Collections1: Collections2: Collections3:
11.00 111.00 111.00
22.00 222.00 222.00
33.00 333.00 333.00
444.00 4444.00 444.00
555.00 5555.00 555.00
666.00 6666.00 666.00
Desired output:
Collections1: Collections2: Collections3:
11.00 111.00 111.00
22.00 222.00 222.00
33.00 333.00 333.00
444.00 4444.00 444.00
555.00 5555.00 555.00
666.00 6666.00 666.00
Have tried aligning right, left, but nothing seems to work.
Edit 1: Believe I may have found a good enough solution.
cout << left << "\t" setw(14) << collections1 << setw(18) << collections2 << setw(15) << collections3 << endl ;
Not certain if this is poor or not, but it gets the job done for the moment.
CodePudding user response:
You can try using left function for streaming your output as below:
std::cout.width(6); std::cout << std::left << n << '\n';
CodePudding user response:
Needing to align floats by the first digit, not by the decimal.
You can use std::left, std::setw and std::setfill as shown below:
int main()
{
std::vector<std::vector<double>> vec{{10.0233, 122.1, 1203.1},{100.03, 22.15, 3.01},{107.03, 152.1, 0.1},{6686.0,6666.00,666.00}};
for(std::vector<double> tempVec: vec)
{
for(double elem: tempVec)
{
std::cout << std::left<<std::setw(8) << std::setfill(' ') << std::fixed << std::setprecision(2) << elem << " ";
}
std::cout<< std::endl;
}
return 0;
}
The output of the above program is :
10.02 122.10 1203.10
100.03 22.15 3.01
107.03 152.10 0.10
6686.00 6666.00 666.00
