title: Is there a reliable way in JavaScript to obtain the number of decimal places of an arbitrary number?
body: <p>It's important to note that I'm not looking for a rounding function. I am looking for a function that returns the number of decimal places in an arbitrary number's simplified decimal representation. That is, we have the following:</p>
  
  <pre><code>decimalPlaces(5555.0);     //=&gt; 0
  decimalPlaces(5555);       //=&gt; 0
  decimalPlaces(555.5);      //=&gt; 1
  decimalPlaces(555.50);     //=&gt; 1
  decimalPlaces(0.0000005);  //=&gt; 7
  decimalPlaces(5e-7);       //=&gt; 7
  decimalPlaces(0.00000055); //=&gt; 8
  decimalPlaces(5.5e-7);     //=&gt; 8
  </code></pre>
  
  <p>My first instinct was to use the string representations: split on <code>'.'</code>, then on <code>'e-'</code>, and do the math, like so (the example is verbose):</p>
  
  <pre><code>function decimalPlaces(number) {
    var parts = number.toString().split('.', 2),
      integerPart = parts[0],
      decimalPart = parts[1],
      exponentPart;
  
    if (integerPart.charAt(0) === '-') {
      integerPart = integerPart.substring(1);
    }
  
    if (decimalPart !== undefined) {
      parts = decimalPart.split('e-', 2);
      decimalPart = parts[0];
    }
    else {
      parts = integerPart.split('e-', 2);
      integerPart = parts[0];
    }
    exponentPart = parts[1];
  
    if (exponentPart !== undefined) {
      return integerPart.length +
        (decimalPart !== undefined ? decimalPart.length : 0) - 1 +
        parseInt(exponentPart);
    }
    else {
      return decimalPart !== undefined ? decimalPart.length : 0;
    }
  }
  </code></pre>
  
  <p>For my examples above, this function works. However, I'm not satisfied until I've tested every possible value, so I busted out <code>Number.MIN_VALUE</code>.</p>
  
  <pre><code>Number.MIN_VALUE;                      //=&gt; 5e-324
  decimalPlaces(Number.MIN_VALUE);       //=&gt; 324
  
  Number.MIN_VALUE * 100;                //=&gt; 4.94e-322
  decimalPlaces(Number.MIN_VALUE * 100); //=&gt; 324
  </code></pre>
  
  <p>This looked reasonable at first, but then on a double take I realized that <code>5e-324 * 10</code> should be <code>5e-323</code>! And then it hit me: I'm dealing with the effects of quantization of very small numbers. Not only are numbers being quantized before storage; additionally, some numbers stored in binary have unreasonably long decimal representations, so their decimal representations are being truncated. This is unfortunate for me, because it means that I can't get at their true decimal precision using their string representations.</p>
  
  <p>So I come to you, StackOverflow community. Does anyone among you know a reliable way to get at a number's true post-decimal-point precision?</p>
  
  <p>The purpose of this function, should anyone ask, is for use in another function that converts a float into a simplified fraction (that is, it returns the relatively coprime integer numerator and nonzero natural denominator). The only missing piece in this outer function is a reliable way to determine the number of decimal places in the float so I can multiply it by the appropriate power of 10. Hopefully I'm overthinking it.</p>