Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7577707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:15:26+00:00 2026-05-30T17:15:26+00:00

It’s important to note that I’m not looking for a rounding function. I am

  • 0

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:

decimalPlaces(5555.0);     //=> 0
decimalPlaces(5555);       //=> 0
decimalPlaces(555.5);      //=> 1
decimalPlaces(555.50);     //=> 1
decimalPlaces(0.0000005);  //=> 7
decimalPlaces(5e-7);       //=> 7
decimalPlaces(0.00000055); //=> 8
decimalPlaces(5.5e-7);     //=> 8

My first instinct was to use the string representations: split on '.', then on 'e-', and do the math, like so (the example is verbose):

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;
  }
}

For my examples above, this function works. However, I’m not satisfied until I’ve tested every possible value, so I busted out Number.MIN_VALUE.

Number.MIN_VALUE;                      //=> 5e-324
decimalPlaces(Number.MIN_VALUE);       //=> 324

Number.MIN_VALUE * 100;                //=> 4.94e-322
decimalPlaces(Number.MIN_VALUE * 100); //=> 324

This looked reasonable at first, but then on a double take I realized that 5e-324 * 10 should be 5e-323! 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.

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?

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T17:15:27+00:00Added an answer on May 30, 2026 at 5:15 pm

    Historical note: the comment thread below may refer to first and second implementations. I swapped the order in September 2017 since leading with a buggy implementation caused confusion.

    If you want something that maps "0.1e-100" to 101, then you can try something like

    function decimalPlaces(n) {
      // Make sure it is a number and use the builtin number -> string.
      var s = "" + (+n);
      // Pull out the fraction and the exponent.
      var match = /(?:\.(\d+))?(?:[eE]([+\-]?\d+))?$/.exec(s);
      // NaN or Infinity or integer.
      // We arbitrarily decide that Infinity is integral.
      if (!match) { return 0; }
      // Count the number of digits in the fraction and subtract the
      // exponent to simulate moving the decimal point left by exponent places.
      // 1.234e+2 has 1 fraction digit and '234'.length -  2 == 1
      // 1.234e-2 has 5 fraction digit and '234'.length - -2 == 5
      return Math.max(
          0,  // lower limit.
          (match[1] == '0' ? 0 : (match[1] || '').length)  // fraction length
          - (match[2] || 0));  // exponent
    }
    

    According to the spec, any solution based on the builtin number->string conversion can only be accurate to 21 places beyond the exponent.

    9.8.1 ToString Applied to the Number Type

    1. Otherwise, let n, k, and s be integers such that k ≥ 1, 10k−1 ≤ s < 10k, the Number value for s × 10n−k is m, and k is as small as possible. Note that k is the number of digits in the decimal representation of s, that s is not divisible by 10, and that the least significant digit of s is not necessarily uniquely determined by these criteria.
    2. If k ≤ n ≤ 21, return the String consisting of the k digits of the decimal representation of s (in order, with no leading zeroes), followed by n−k occurrences of the character ‘0’.
    3. If 0 < n ≤ 21, return the String consisting of the most significant n digits of the decimal representation of s, followed by a decimal point ‘.’, followed by the remaining k−n digits of the decimal representation of s.
    4. If −6 < n ≤ 0, return the String consisting of the character ‘0’, followed by a decimal point ‘.’, followed by −n occurrences of the character ‘0’, followed by the k digits of the decimal representation of s.

    Historical note: The implementation below is problematic. I leave it here as context for the comment thread.

    Based on the definition of Number.prototype.toFixed, it seems like the following should work but due to the IEEE-754 representation of double values, certain numbers will produce false results. For example, decimalPlaces(0.123) will return 20.

    function decimalPlaces(number) {
      // toFixed produces a fixed representation accurate to 20 decimal places
      // without an exponent.
      // The ^-?\d*\. strips off any sign, integer portion, and decimal point
      // leaving only the decimal fraction.
      // The 0+$ strips off any trailing zeroes.
      return ((+number).toFixed(20)).replace(/^-?\d*\.?|0+$/g, '').length;
    }
    
    // The OP's examples:
    console.log(decimalPlaces(5555.0));  // 0
    console.log(decimalPlaces(5555));  // 0
    console.log(decimalPlaces(555.5));  // 1
    console.log(decimalPlaces(555.50));  // 1
    console.log(decimalPlaces(0.0000005));  // 7
    console.log(decimalPlaces(5e-7));  // 7
    console.log(decimalPlaces(0.00000055));  // 8
    console.log(decimalPlaces(5e-8));  // 8
    console.log(decimalPlaces(0.123));  // 20 (!)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a function that will clean a strings' special characters. I do NOT
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a jquery bug and I've been looking for hours now, I can't
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I want to construct a data frame in an Rcpp function, but when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.