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. Algorithms
  4. algorithm to get each individual digit from a decimal number (integer)

algorithm to get each individual digit from a decimal number (integer)

Scheduled Pinned Locked Moved Algorithms
algorithmshelptutorial
25 Posts 9 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.
  • A ant damage

    What the purpose of this algorithm? Do you ever saw those 7-segment led displays in your tv, stereo hi-fi, vcr or dvd? I have one of those but with 4 digits in one display, and those digits are in multiplexed mode, so I can only iterate with one at a time. I won't use negative numbers, because I will use that display to make a digital counter/timer. By the way, thanks for the example. I'm now ready to create an optimized one.

    L Offline
    L Offline
    Luc Pattyn
    wrote on last edited by
    #21

    Oh great. This thread holds 20 messages already, and now you come with some details. I'm out of here. X|

    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


    I only read formatted code with indentation, so please use PRE tags for code snippets.


    I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


    1 Reply Last reply
    0
    • A ant damage

      What the purpose of this algorithm? Do you ever saw those 7-segment led displays in your tv, stereo hi-fi, vcr or dvd? I have one of those but with 4 digits in one display, and those digits are in multiplexed mode, so I can only iterate with one at a time. I won't use negative numbers, because I will use that display to make a digital counter/timer. By the way, thanks for the example. I'm now ready to create an optimized one.

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #22

      Please don't remove messages that have replies attached; it is against the forum guidelines. I copied your original question here: Hi I need help to find the best snippet to extract individual digits from an integer. Here is an example of what I want to do:

      int number = 1024; //this is the number I want to split into digits
      int digit1 = 1;
      int digit2 = 0;
      int digit3 = 2;
      int digit4 = 4;

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      I only read formatted code with indentation, so please use PRE tags for code snippets.


      I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


      T 1 Reply Last reply
      0
      • D Dr Walt Fair PE

        "Best" in what way? Since I have no specifications, I'd probably use something quick and dirty like

        int n = 1024; // given number
        int d = 2; // digit number, d < n.ToString().Length
        int ds = int.Parse(n.ToString().Substring(d,1));

        etc.

        CQ de W5ALT

        Walt Fair, Jr., P. E. Comport Computing Specializing in Technical Engineering Software

        A Offline
        A Offline
        Anshul R
        wrote on last edited by
        #23

        Convert the number to string Loop through the characters, store it. Eg : <pre> Dim digits As New ArrayList For Each digit As Char in CStr(number) digits.Add(CInt(digit)) End For </pre>

        1 Reply Last reply
        0
        • L Luc Pattyn

          Please don't remove messages that have replies attached; it is against the forum guidelines. I copied your original question here: Hi I need help to find the best snippet to extract individual digits from an integer. Here is an example of what I want to do:

          int number = 1024; //this is the number I want to split into digits
          int digit1 = 1;
          int digit2 = 0;
          int digit3 = 2;
          int digit4 = 4;

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          I only read formatted code with indentation, so please use PRE tags for code snippets.


          I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


          T Offline
          T Offline
          Tadeusz Westawic
          wrote on last edited by
          #24

          int source int digit while source { digit = source % 10 output digit source /= 10 } get u started Tadeusz Westawic Sum quid sum.

          L 1 Reply Last reply
          0
          • T Tadeusz Westawic

            int source int digit while source { digit = source % 10 output digit source /= 10 } get u started Tadeusz Westawic Sum quid sum.

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #25

            there is no need to start this thread all over again. My message to the OP was: don't remove what you wrote, which, for the curious, is: "..." :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

            Please use < PRE > tags for code snippets, it preserves indentation, and improves readability.

            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