How do I make a double take up a known amount of characters in a stream?

So I have the following stream:

1
2
3
4
5
6
7
8
ss << 
	"TpE:" << setprecision(2) << double(clock() - start_of_last_cycle) / (double) CLOCKS_PER_SEC << " " <<
	"BpC:" << setprecision(2) << av_brains_per_community << " " <<
	"SpB:" << setprecision(2) << av_synapses_per_brain << " " <<
	"HNpB:" << setprecision(2) << av_hidden_neurons_per_brain << " " <<
	"SpHN:" << setprecision(2) << av_synapses_per_hidden_neuron << " " <<
	champion->pT->report(champion->error) <<
	endl;


which looks like this when written to the screen:

X(E102): TpE:3e+002 BpC:62 SpB: 3.2 HNpB:3.2 SpHN:1 H24 error: 37.01677972 %


I am writing this on the screen at every simulation step, to track the progress of my program. When viewing the data, it's helpful to see changes as they progress, so ideally I would like to force every element in the list to take up a known amount of space. Unfortunately, setprecision() does the following:

1
2
3
4
5
cout << 
	setprecision(2) << 2.0 <<
	setprecision(2) << 2.1 <<
         setprecision(2) << 2100 <<
	endl;


2
2.1
2.1e+003


When really I want this:

2.0e+000
2.1e+000
2.1e+003


or, better yet, I want to be able to do this:

2        some text
2.1      some text
2.1e+003 some text


Is this possible ( I realise that last one might be a bit tricky)? Also, how can I add leading zeros to integers, such that 230 becomes 0230 and 1 becomes 0001

Thanks for the help :)
Last edited on
bump
1
2
3
4
5
6
7
        std::cout << std::fixed << std::setprecision(2) << std::showpoint ;

        // std::setw must be specified each time
        std::cout << std::setw(10) << 2.0  << '\n'
                  << std::setw(10) << 2.1 << '\n'
                  << std::setw(10) << 2100.0 << '\n'
                  << std::setw(10) << 12345.6789 << '\n' ;


Output:
      2.00
      2.10
   2100.00
  12345.68


cool, thanks :)
Topic archived. No new replies allowed.