title: How to correctly multiply a float with an int and get a result only influenced by significant digits?
body: <p>I have code that converts between a float (representing a second) and an int64 (representing a nanosecond), <em>taking <code>6</code> decimal places from the float</em></p>
  
  <pre><code>int64_t nanos = f * 1000000000LL;
  </code></pre>
  
  <p>However many decimal values stored in floats cannot be represented exactly in the binary float, so I get results like <code>14199999488</code> when my float is <code>14.2f</code>. Currently I solve this issue by computing the significant number of digits after the radix point</p>
  
  <pre><code>const float logOfSecs = std::log10(f);
  
  int precommaPlaces = 0;
  if(logOfSecs &gt; 0) {
     precommaPlaces = std::ceil(logOfSecs);
  }
  
  int postcommaPlaces = 7 - precommaPlaces;
  if(postcommaPlaces &lt; 0) {
     postcommaPlaces = 0;
  }
  </code></pre>
  
  <p>And then printing the float into a string to let Qt round the float correctly. Then I parse the string into a pre and post comma integer and multiple them with integer arithmetic. </p>
  
  <pre><code>const QString valueStr = QString::number(f, 'f', postcommaPlaces);
  qint64 nanos = 0;
  nanos += valueStr.section(".", 0, 0).toLongLong() * 1000000000LL;
  if(postcommaPlaces) {
     nanos += valueStr.section(".", 1).toLongLong() * 
       std::pow(10.0, 9 - postcommaPlaces);
  }
  </code></pre>
  
  <p>This works fine, but I was wondering whether there is a better, perhaps faster way to do this?</p>