title: Use sprintf to format floats with no decimal places if integer
body: <p>Originally I was using sprintf with floats always with 2 decimal places using the following code:</p>
  
  <pre><code>static void MyFunc(char* buffer, const float percentage)
  {
      sprintf(buffer, "%.2f", percentage);
  }
  </code></pre>
  
  <p>One of the percentage values passed was 0x419FFFFF 20 (debugger view), this printed 20.00 into buffer.</p>
  
  <p>I would like instead to show 2 decimal places when not an integer, e.g.</p>
  
  <pre><code>94.74 displayed as 94.74
  94.7  displayed as 94.70
  0     displayed as 0
  5     displayed as 5
  100   displayed as 100
  </code></pre>
  
  <p>I am currently using the following code:</p>
  
  <pre><code>static void MyFunc(char* buffer, const float percentage)
  {
      int fractional_part = ((percentage - (int)percentage) * 100);
      if (0 == fractional_part)
      {
          sprintf(buffer, "%d", (int)percentage);
      }
      else
      {
          sprintf(buffer, "%.2f", percentage);
      }
  }
  </code></pre>
  
  <p>Now if 0x419FFFFF 20 (debugger view) is passed, fractional part is calculated as 99. I assume then the sum for fractional_part ends up being (19.99 - 19) * 100 = 99. Why then does the first example not print 19.99 into buffer?</p>
  
  <p>What is the correct solution for my problem?</p>