length of decimal expansion

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vix290788
    New Member
    • Oct 2008
    • 4

    length of decimal expansion

    How do I go about writing a code to determine how many digits are in a decimal expansion of any given number?
    I have entered the
    cin >> N;
    so that the user enters any number, but how do I calculate the number of digits they enter?
  • curiously enough
    New Member
    • Aug 2008
    • 79

    #2
    If you want the number of digits AFTER the decimal there are countless ways to find them. This a random one I thought up:
    Code:
    Spoonfeeding code removed
    If you want the number of digits BEFORE the decimal there are even more ways, like:
    Code:
    Spoonfeeding code removed
    The integer i represents in both cases the number of digits.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      You could input a string and later convert the string into a number. It should be straightforward to convert a string into a number. Finding the length of a string is trivial.

      Don't count leading and trailing whitespace as decimal digits.
      Don't count the decimal point as a decimal digit.

      Comment

      Working...