Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Dividing a number into individual digits

Reply
Thread Tools

Dividing a number into individual digits

 
 
Timothy Baron
Guest
Posts: n/a
 
      09-21-2010
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/.

 
Reply With Quote
 
 
 
 
Edmond Kachale
Guest
Posts: n/a
 
      09-21-2010
2010/9/21 Timothy Baron <(E-Mail Removed)>

> 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*

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      09-21-2010
On Tue, Sep 21, 2010 at 1:33 PM, Timothy Baron <(E-Mail Removed)> 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/

 
Reply With Quote
 
Timothy Baron
Guest
Posts: n/a
 
      09-21-2010
Robert Klemme wrote:
> On Tue, Sep 21, 2010 at 1:33 PM, Timothy Baron <(E-Mail Removed)>
> 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/.

 
Reply With Quote
 
(r.*n){2}
Guest
Posts: n/a
 
      09-23-2010
On Sep 21, 8:31*am, Timothy Baron <(E-Mail Removed)> wrote:
> Robert Klemme wrote:
> > On Tue, Sep 21, 2010 at 1:33 PM, Timothy Baron <(E-Mail Removed)>
> > 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 )
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Using %x to format number to hex and number of digits Tim Chase Python 2 11-06-2010 01:22 AM
Re: Using %x to format number to hex and number of digits Chris Rebert Python 1 11-05-2010 07:05 PM
number of digits in a number bejiz C++ 12 09-27-2007 09:21 PM
Dividing 1 by a number k ferbar C Programming 4 09-27-2005 07:09 AM
Dividing a large wav file into smaller pieces Marco Bakker Computer Support 3 08-21-2004 09:34 AM



Advertisments