Dividing a number into individual digits

Discussion in 'Ruby' started by Timothy Baron, Sep 21, 2010.

  1. Here's a simple question: What's an effecient way to get the digits of
    a number. For instance, if I have the number 37, how can I get "3" and
    "7"?
    --
    Posted via http://www.ruby-forum.com/.
    Timothy Baron, Sep 21, 2010
    #1
    1. Advertising

  2. 2010/9/21 Timothy Baron <>

    > Here's a simple question: What's an effecient way to get the digits of
    > a number. For instance, if I have the number 37, how can I get "3" and
    > "7"?
    >

    I'm not sure about the efficiency but this can do:
    number.to_s.split(//)
    Ex: 1464002.to_s.split(//) =3D> ["1", "4", "6", "4", "0", "0", "2"]

    Otherwise, you may need to derive a method of your own. :)


    ---
    Edmond
    Software Developer | Baobab Health Trust (http://www.baobabhealth.org/) |
    Malawi

    Cell: +265 999 465 137 | +265 881 234 717

    *"Many people doubt open source software and probably don=92t realize that
    there is an alternative=85 which is just as good.." -- Kevin Scannell*
    Edmond Kachale, Sep 21, 2010
    #2
    1. Advertising

  3. On Tue, Sep 21, 2010 at 1:33 PM, Timothy Baron <> wr=
    ote:
    > Here's a simple question: =A0What's an effecient way to get the digits of
    > a number. =A0For instance, if I have the number 37, how can I get "3" and
    > "7"?


    Five days ago the exact same question was raised here - and answered.
    What prevented you finding that thread?

    Cheers

    robert


    --=20
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Sep 21, 2010
    #3
  4. Robert Klemme wrote:
    > On Tue, Sep 21, 2010 at 1:33 PM, Timothy Baron <>
    > wrote:
    >> Here's a simple question: �What's an effecient way to get the digits of
    >> a number. �For instance, if I have the number 37, how can I get "3" and
    >> "7"?

    >
    > Five days ago the exact same question was raised here - and answered.
    > What prevented you finding that thread?
    >
    > Cheers
    >
    > robert


    An inability to use the right search phrase? I had looked, but nothing
    relevant came up. Thanks for letting me know about this other thread,
    though. Just found it, and it answers the question nicely.
    --
    Posted via http://www.ruby-forum.com/.
    Timothy Baron, Sep 21, 2010
    #4
  5. Timothy Baron

    (r.*n){2} Guest

    On Sep 21, 8:31 am, Timothy Baron <> wrote:
    > Robert Klemme wrote:
    > > On Tue, Sep 21, 2010 at 1:33 PM, Timothy Baron <>
    > > wrote:
    > >> Here's a simple question: What's an effecient way to get the digits of
    > >> a number. For instance, if I have the number 37, how can I get "3" and
    > >> "7"?

    >
    > > Five days ago the exact same question was raised here - and answered.
    > > What prevented you finding that thread?

    >
    > > Cheers

    >
    > > robert

    >
    > An inability to use the right search phrase?  I had looked, but nothing
    > relevant came up.  Thanks for letting me know about this other thread,
    > though.  Just found it, and it answers the question nicely.
    > --
    > Posted viahttp://www.ruby-forum.com/.


    One alternative to split could be unpack

    s = "12345" * 2000

    s.unpack('C*').each {|d| printf("%c\n", d)}

    note that s contains ascii values for each digit obtained ( '0' =>
    48, ..., '9' => 57 )
    (r.*n){2}, Sep 23, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ferbar

    Dividing 1 by a number k

    ferbar, Sep 27, 2005, in forum: C Programming
    Replies:
    4
    Views:
    283
    Martin Ambuhl
    Sep 27, 2005
  2. bejiz
    Replies:
    12
    Views:
    607
  3. Radek

    Dividing task into processors

    Radek, Oct 16, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    279
  4. Chris Rebert
    Replies:
    1
    Views:
    385
    Grant Edwards
    Nov 5, 2010
  5. Tim Chase
    Replies:
    2
    Views:
    552
    Tim Chase
    Nov 6, 2010
Loading...

Share This Page