title: Truncate C++ string fields generated by ostringstream, iomanip:setw
body: <p>In C++ I need string representations of integers with leading zeroes, where the representation has 8 digits and no more than 8 digits, truncating digits on the right side if necessary.  I thought I could do this using just ostringstream and iomanip.setw(), like this:</p>
  
  <pre><code>int num_1 = 3000;
  ostringstream out_target;
  
  out_target &lt;&lt; setw(8) &lt;&lt; setfill('0') &lt;&lt; num_1;
  cout &lt;&lt; "field: " &lt;&lt; out_target.str() &lt;&lt; " vs input: " &lt;&lt; num_1 &lt;&lt; endl;
  </code></pre>
  
  <p>The output here is:</p>
  
  <pre><code>field: 00003000 vs input: 3000
  </code></pre>
  
  <p>Very nice! However if I try a bigger number, setw lets the output grow beyond 8 characters:</p>
  
  <pre><code>int num_2 = 2000000000;
  ostringstream out_target;
  
  out_target &lt;&lt; setw(8) &lt;&lt; setfill('0') &lt;&lt; num_2;
  cout &lt;&lt; "field: " &lt;&lt; out_target.str() &lt;&lt; " vs input: " &lt;&lt; num_2 &lt;&lt; endl;
  out_target.str("");
  </code></pre>
  
  <p>output:</p>
  
  <pre><code>field: 2000000000 vs input: 2000000000
  </code></pre>
  
  <p>The desired output is "20000000".  There's nothing stopping me from using a second operation to take only the first 8 characters, but is field truncation truly missing from iomanip?  Would the Boost formatting do what I need in one step?</p>