Question: Counting integers

I 've come across this somewhere but right now it's escapeing me.

x = 100 , so I want to find out how many digits are in that number.

??
Tia
Stuart

x.to_s.length will do it

···

On 6/26/06, Dark Ambient <sambient@gmail.com> wrote:

I 've come across this somewhere but right now it's escapeing me.

x = 100 , so I want to find out how many digits are in that number.

??
Tia
Stuart

--
Phillip Hutchings
http://www.sitharus.com/

Dark Ambient wrote:

I 've come across this somewhere but right now it's escapeing me.

x = 100 , so I want to find out how many digits are in that number.

??
Tia
Stuart

irb(main):001:0> x = 100
=> 100
irb(main):002:0> x.to_s.length
=> 3

Be careful though if x can be something diffrent from a non negative integer.

robert

···

2006/6/26, Timothy Hunter <TimHunter@nc.rr.com>:

irb(main):001:0> x = 100
=> 100
irb(main):002:0> x.to_s.length
=> 3

--
Have a look: http://www.flickr.com/photos/fussel-foto/

From: Timothy Hunter <TimHunter@nc.rr.com>
Reply-To: ruby-talk@ruby-lang.org
To: ruby-talk@ruby-lang.org (ruby-talk ML)
Subject: Re: Question: Counting integers
Date: Mon, 26 Jun 2006 09:35:26 +0900

Dark Ambient wrote:

I 've come across this somewhere but right now it's escapeing me.

x = 100 , so I want to find out how many digits are in that number.

??
Tia
Stuart

irb(main):001:0> x = 100
=> 100
irb(main):002:0> x.to_s.length
=> 3

irb(main):001:0> x = 100
=> 100
irb(main):002:0> Math.log10(x+1).ceil
=> 3

Regards,

Park Heesob

irb(main):003:0> x=0
=> 0
irb(main):004:0> Math.log10(x+1).ceil
=> 0
irb(main):005:0> x.to_s.scan(/\d/).length
=> 1
irb(main):006:0> x=100
=> 100
irb(main):007:0> x.to_s.scan(/\d/).length
=> 3

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
+1 513-295-4739

···

On Jun 26, 2006, at 4:46 AM, Park Heesob wrote:

From: Timothy Hunter <TimHunter@nc.rr.com>
Reply-To: ruby-talk@ruby-lang.org
To: ruby-talk@ruby-lang.org (ruby-talk ML)
Subject: Re: Question: Counting integers
Date: Mon, 26 Jun 2006 09:35:26 +0900

Dark Ambient wrote:

I 've come across this somewhere but right now it's escapeing me.

x = 100 , so I want to find out how many digits are in that number.

??
Tia
Stuart

irb(main):001:0> x = 100
=> 100
irb(main):002:0> x.to_s.length
=> 3

irb(main):001:0> x = 100
=> 100
irb(main):002:0> Math.log10(x+1).ceil
=> 3

Regards,

Park Heesob