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

On 6/26/06, Dark A. [email protected] 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

x.to_s.length will do it

Dark A. 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

??
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 H.

2006/6/26, Timothy H. [email protected]:

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

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

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

Regards,

Park H.

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 B. http://agileconsultingllc.com
[email protected]
+1 513-295-4739

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs