I want find the length
of a Fixnum
, num
, without converting it into a String
.
In other words, how many digits are in num
without calling the .to_s()
method:
num.to_s.length
puts Math.log10(1234).to_i + 1 # => 4
You could add it to Fixnum like this:
class Fixnum
def num_digits
Math.log10(self).to_i + 1
end
end
puts 1234.num_digits # => 4