jeudi 3 février 2022

align the first digit for floats C++

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.

Aucun commentaire:

Enregistrer un commentaire