title: How to properly set precision for doubles in C++
body: <p>I'm working on a project where I need to do some math and give the user output with dollars in it, so I would like to have my console tell the user an answer like <code>$20.15</code> instead of <code>$20.153</code>. I used the set precision function as such:
  <code>cout &lt;&lt; setprecision(2);</code>, but rather than have the numbers become what I want them to be, they are converted into scientific notation.</p>
  
  <p>I'm outputting a lot of numbers, so having a function like <code>setprecision</code> would be best for me for ease of use.</p>
  
  <p>How do I properly have the numbers displayed with only two decimal places and not have the console give me numbers in scientific notation?</p>
  
  <p>Thanks</p>
  
  <p>Nathan</p>
  
  <p>EDIT:</p>
  
  <p>Here is the part of my code I'm having problems with:</p>
  
  <pre><code>int main() {
  
  cout &lt;&lt; setprecision(2);
  
  if (totalCostHybrid &lt; totalCostNonHybrid) {
              cout &lt;&lt; "Hybrid car: " &lt;&lt; endl;
              cout &lt;&lt; "Total cost: " &lt;&lt; totalCostHybrid &lt;&lt; endl;
              cout &lt;&lt; "Total gallons used: " &lt;&lt; milesPerYear / hybridEffic &lt;&lt; endl;
              cout &lt;&lt; "Total gas cost: " &lt;&lt; gasCostHybrid &lt;&lt; endl;
              cout &lt;&lt; "Non-hybrid car: " &lt;&lt; endl;
              cout &lt;&lt; "Total cost: " &lt;&lt; totalCostNonHybrid &lt;&lt; endl;
              cout &lt;&lt; "Total gallons used: " &lt;&lt; milesPerYear / nonHybridEffic &lt;&lt; endl;
              cout &lt;&lt; "Total gas cost: " &lt;&lt; gasCostNonHybrid &lt;&lt; endl;
              cout &lt;&lt; "Hybrid is cheaper!" &lt;&lt; endl;
  }
  </code></pre>
  
  <p>Obviously there's more to it, but this is what I need help with.</p>