title: How can I format a float to a certain number of decimal and integer digits?
body: <p>I am trying to format a float in Ruby to exactly four digits, including the decimal.</p>
  
  <p>For instance:</p>
  
  <pre><code>1 =&gt; 01.00
  2.4 =&gt; 02.40
  1.4455 =&gt; 01.45
  </code></pre>
  
  <p>Right now, I am trying to format the float as follows:</p>
  
  <pre><code>str_result = " %.2f " %result
  </code></pre>
  
  <p>Which successfully limits the decimal places to two.</p>
  
  <p>I'm also aware of:</p>
  
  <pre><code>str_result = " %2d " %result
  </code></pre>
  
  <p>Which succesfully turns <code>1</code> into <code>01</code>, but loses the decimal places.</p>
  
  <p>I tried to combine these like so:</p>
  
  <pre><code>str_result = " %2.2f " %result
  </code></pre>
  
  <p>to no apparent effect. It has the same results as <code>%.2f</code>.</p>
  
  <p>Is there a way to force Ruby to format the string into this four digit format?</p>