From: Florian Gilcher Date: 2009-02-24T08:26:52+09:00 Subject: [ruby-core:22389] Re: Proposal: Integer.digit_count --Apple-Mail-21--573595556 Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes Content-Transfer-Encoding: 7bit > > > On Mon, Feb 23, 2009 at 4:07 PM, Kurt Stephens > wrote: > -1.digit_count : is '-' a digit? Fixnum#log10 anyone? Actually, that was my first thought as well. The problem is that it gets a bit more verbose: class Fixnum def digit_count if self == 0 1 #define how many digits 0 has, because log(0) is undefined else Math.log10(self.abs).floor + 1 end end end And: the #to_s-version is a bit faster, but that doesn't matter much. I think abs.to_s is okay for most implementations. You shouldn't use it in a scientific application, though ;). Regards, Florian -- Florian Gilcher smtp: flo@andersground.net jabber: Skade@jabber.ccc.de gpg: 533148E2 --Apple-Mail-21--573595556 Content-Type: text/html; charset=US-ASCII Content-Transfer-Encoding: quoted-printable


On Mon, Feb = 23, 2009 at 4:07 PM, Kurt Stephens <kurt@cashnetusa.com> = wrote:
-1.digit_count : is = '-' a digit?  Fixnum#log10 = anyone?

Actually, that was my first thought as well. The problem is that it = gets a bit more verbose:

class = Fixnum
  def digit_count
   =  if self =3D=3D 0
      1 =  #define how many digits 0 has, because log(0) is = undefined
    else
   =    Math.log10(self.abs).floor + 1
   =  end
  end
end

And: the #to_s-version is a bit faster, but that doesn't matter much. I = think abs.to_s is okay for most implementations. You shouldn't use it in = a scientific application, though = ;).

Regards,
Florian

--
Florian = Gilcher

gpg:=    533148E2
=

= --Apple-Mail-21--573595556--