Class: Fixnum

Inherits:
Object show all
Defined in:
lib/shenanigans/fixnum/string_length.rb

Instance Method Summary collapse

Instance Method Details

#string_lengthObject

Returns the length of the number's string representation.

0.string_length
#=> 1
123.string_length
#=> 3
-1.string_length
#=> 2

9
10
11
12
13
# File 'lib/shenanigans/fixnum/string_length.rb', line 9

def string_length
  return 1 if self.zero?
  len = Math.log10(self.abs).floor.next
  self > 0 ? len : len.next
end