title: How does printf extract digits from a floating point number?
body: <p>How do functions such as <code>printf</code> extract digits from a floating point number? I understand how this could be done in principle. Given a number <code>x</code>, of which you want the first <code>n</code> digits, scale <code>x</code> by a power of 10 so that <code>x</code> is between <code>pow(10, n)</code> and <code>pow(10, n-1)</code>. Then convert <code>x</code> into an integer, and take the digits of the integer. </p>
  
  <p>I tried this, and it worked. Sort of. My answer was identical to the answer given by <code>printf</code> for the first 16 decimal digits, but tended to differ on the ones after that. How does <code>printf</code> do it?</p>