Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. number of decimals in integer

number of decimals in integer

Scheduled Pinned Locked Moved C / C++ / MFC
algorithmshelp
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jan Hirak
    wrote on last edited by
    #1

    Is there any function which returns number of decimals in the integer, or can sombody help me with fast algorithm to do this. thanks

    P M G 3 Replies Last reply
    0
    • J Jan Hirak

      Is there any function which returns number of decimals in the integer, or can sombody help me with fast algorithm to do this. thanks

      P Offline
      P Offline
      PJ Arends
      wrote on last edited by
      #2

      int digits = log10(integer) + 1;

      Only works with positive numbers. HTH


      [

      ](http://www.canucks.com)Sonork 100.11743 Chicken Little "You're obviously a superstar." - Christian Graus about me - 12 Feb '03 Within you lies the power for good - Use it!

      1 Reply Last reply
      0
      • J Jan Hirak

        Is there any function which returns number of decimals in the integer, or can sombody help me with fast algorithm to do this. thanks

        M Offline
        M Offline
        Mike Nordell
        wrote on last edited by
        #3

        pepevejlupek wrote: Is there any function which returns number of decimals in the integer Trick questions should probably be directed to the lounge. The number of decimals in an integer is always zero.

        G 1 Reply Last reply
        0
        • J Jan Hirak

          Is there any function which returns number of decimals in the integer, or can sombody help me with fast algorithm to do this. thanks

          G Offline
          G Offline
          Gary R Wheeler
          wrote on last edited by
          #4

          int CountDigits(int value)
          {
          int digit_count = 0;

          do {
              digit\_count++;
              value /= 10;
          } while (value != 0) {
          
          return digit\_count;
          

          }

          or, even faster:

          int CountDigitsFaster(int value)
          {
          int digit_count = 1;
          if (value < 0) value = -value;
          if (value > 9) digit_count++;
          if (value > 99) digit_count++;
          if (value > 999) digit_count++;
          if (value > 9999) digit_count++;
          if (value > 99999) digit_count++;
          if (value > 999999) digit_count++;
          if (value > 9999999) digit_count++;
          if (value > 99999999) digit_count++;
          if (value > 999999999) digit_count++;
          return digit_count;
          }

          Note that CountDigitsFaster assumes that int's are 32-bit signed values. Both functions assume that the minus sign in negative values is not considered a 'digit'. If you're actually trying to compute the number of characters required to display the value, then you would have to take that into account as well. CountDigitsFaster looks like dumb code, but is significantly faster than any alternative I can think of. It requires 10 compares, 10 jumps, possibly a negation, and up to 9 increments. My original CountDigits, while it may exit with an early out for small values, requires an increment and a division per iteration, for up to 9 iterations.


          Software Zen: delete this;

          1 Reply Last reply
          0
          • M Mike Nordell

            pepevejlupek wrote: Is there any function which returns number of decimals in the integer Trick questions should probably be directed to the lounge. The number of decimals in an integer is always zero.

            G Offline
            G Offline
            Gary R Wheeler
            wrote on last edited by
            #5

            Subtle, dude :cool:.


            Software Zen: delete this;

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups