title: Getting the formatting right with sprintf()
body: <p>I'm trying to get some formatting done in my sprintf statement, but it doesn't seem to work as I expect. Here is the line:</p>
  
  <pre><code>n = sprintf(buffer, "x = %d.%d, y = %d.%d, z = %d.%d \n", x1, x2, y1, y2, z1, z2);
  </code></pre>
  
  <p>In that printout x1 is the whole part of the number, and x2 is the fractional part. All would be well, except I need to pad x2, y2, and z2 to always be 2 digits - meaning I need to pad with leading zeros.</p>
  
  <p>With examples that I see online it seems like doing this should work:</p>
  
  <pre><code>n = sprintf(buffer, "x = %d.%02d, y = %d.%02d, z = %d.%02d \n", x1, x2, y1, y2, z1, z2);
  </code></pre>
  
  <p>However, that instead produces something like this:</p>
  
  <pre><code>x = 2.2d, y = 37.2d, z = 2.2d
  </code></pre>
  
  <p>The 37 above is actually x2, and it apparently got shifted over in place of y1. I tried putting brackets around the '02', but that doesn't do anything either. </p>
  
  <p>I have tried splitting up the period too like this: (but that didn't work)</p>
  
  <pre><code>   n = sprintf(buffer, "x = %d. %02d, y = %d. %02d, z = %d. %02d \n", x1, x2, y1, y2, z1, z2);
  </code></pre>
  
  <p>I'm not really sure what's wrong... I'd appreciate any help. This isn't particularly vital to do in sprintf (I could theoretically write some 'if' statements and get it working that way), but it'd be nice.</p>
  
  <p>Thanks!</p>