title: PHP: Converting dollars to cents
body: <p>As input, I want to accept any of the following: "$12.33", "14.92", "$13", "17", "14.00001". As output, I want 1233, 1492, 1300, 1700 and 1400 respectively. This is apparently not as easy as it looks:</p>
  
  <pre><code>&lt;?php
  $input = '$64.99';  // value is given via form submission
  $dollars = str_replace('$', '', $input);  // get rid of the dollar sign
  $cents = (int)($dollars * 100) // multiply by 100 and truncate
  echo $cents;
  ?&gt;
  </code></pre>
  
  <p>This outputs 6498 instead of 6499.</p>
  
  <p>I assume this has to do with inaccuracies in floating point values, and avoiding these is the whole reason I'm converting to integer cents in the first place. I suppose I could use logic like "get rid of the $ sign, check if there's a decimal point, if so, check how many characters there are after it padding to two and truncating after that then remove the period, if there wasn't one append two zeros and hope for the best" but using string operations for this seems ridiculous.</p>
  
  <p>Surely taking a monetary value from a form and storing it as cents in a database is a common use case. Surely there is a "reasonable" way of doing this.</p>
  
  <p>Right? .....right? :&lt;</p>