#include <iostream> #include <iomanip> int main() { float a = 1.f; std::cout << std::showpoint << a << ' ' << a/3; } 

Output with a different number of characters (7 and 8):

1.00000 0.333333

With other numbers:

 #include <iomanip> int main() { float a = 10.f; // не 1.f std::cout << std::showpoint << a << ' ' << a/3; } 

Output with the same number of characters (7 and 7):

10.0000 3.33333

Why is that? How to always get the same number of characters? I need it for a nice console output / formatted file.

  • setw do not want to use for alignment? All the same align by reducing the number of decimal places - in fact, moving the comma - ugly ... - Harry
  • @Harry how will setw work with float ? It turns out a different number of characters - violet
  • @Harry for example, for a unit there will be 5 characters, and for 1/3 - 8 - violet
  • @Harry should be output exactly under each other as in the table - violet
  • Look here - ideone.com/PHu5xX - Harry

1 answer 1

I would align something like this:

 #include <iostream> #include <iomanip> using namespace std; double v1[] = { 1.0, 33.15, 30/3.0, 2.718281828 }; double v2[] = { 1.0, 33.15*3, 321/3.0, 2.718281828*3.14 }; int main() { for(int i = 0; i < 4; ++i) { cout << fixed << right << setprecision(8) << setw(12) << v1[i] << " | " << setw(12) << v2[i] << endl; } } 

Result:

  1.00000000 | 1.00000000 33.15000000 | 99.45000000 10.00000000 | 107.00000000 2.71828183 | 8.53540494