how to get the length of a number

Discussion in 'Python' started by Stan Cook, Jun 11, 2006.

  1. Stan Cook

    Stan Cook Guest

    Can anyone tell me how to get the length of a number. I
    know len(string) will get the length of a string, but it
    doesn't like len(int). I seem to remember something like %s
    string. I tried to set a variable = to %s int, but that
    doesn't work. Is there a function I've forgotten about to
    convert an integer to a string?

    Regards

    Stan
     
    Stan Cook, Jun 11, 2006
    #1
    1. Advertising

  2. Stan Cook

    Saketh Guest

    Stan Cook wrote:
    > Can anyone tell me how to get the length of a number. I
    > know len(string) will get the length of a string, but it
    > doesn't like len(int). I seem to remember something like %s
    > string. I tried to set a variable = to %s int, but that
    > doesn't work. Is there a function I've forgotten about to
    > convert an integer to a string?
    >
    > Regards
    >
    > Stan


    Use str(int). Then use len(). For example, len(str(12345)) will give
    you 5.
     
    Saketh, Jun 11, 2006
    #2
    1. Advertising

  3. Stan Cook wrote:

    > Is there a function I've forgotten about to convert an
    > integer to a string?


    str(value)

    </F>
     
    Fredrik Lundh, Jun 11, 2006
    #3
  4. Stan Cook

    Stan Cook Guest

    Saketh wrote:
    > Stan Cook wrote:
    >> Can anyone tell me how to get the length of a number. I
    >> know len(string) will get the length of a string, but it
    >> doesn't like len(int). I seem to remember something like %s
    >> string. I tried to set a variable = to %s int, but that
    >> doesn't work. Is there a function I've forgotten about to
    >> convert an integer to a string?
    >>
    >> Regards
    >>
    >> Stan

    >
    > Use str(int). Then use len(). For example, len(str(12345)) will give
    > you 5.
    >

    Ahhhh! How could I have forgotten that. Boy is my face red :)

    Thanks....

    I'll try to make my next question a more intelligent one.

    Regards,

    Stan
     
    Stan Cook, Jun 11, 2006
    #4
  5. Felipe Almeida Lessa enlightened us with:
    > To see how many decimal digits it has:
    >
    > import math
    > math.ceil(math.log(i, 10))


    That doesn't work properly.

    >>> import math
    >>> math.ceil(math.log(10000, 10))

    4.0
    >>> math.ceil(math.log(10001, 10))

    5.0

    But "10000" certainly has as much digits as "10001".

    Sybren
    --
    The problem with the world is stupidity. Not saying there should be a
    capital punishment for stupidity, but why don't we just take the
    safety labels off of everything and let the problem solve itself?
    Frank Zappa
     
    Sybren Stuvel, Jun 11, 2006
    #5
  6. Em Dom, 2006-06-11 às 20:10 +0000, Stan Cook escreveu:
    > Can anyone tell me how to get the length of a number. I
    > know len(string) will get the length of a string, but it
    > doesn't like len(int). I seem to remember something like %s
    > string. I tried to set a variable = to %s int, but that
    > doesn't work. Is there a function I've forgotten about to
    > convert an integer to a string?


    To convert an integer i to a string:

    str(i) or "%s" % i


    To see how many decimal digits it has:

    import math
    math.ceil(math.log(i, 10))

    --
    Felipe.
     
    Felipe Almeida Lessa, Jun 11, 2006
    #6
  7. Stan Cook wrote:
    > Can anyone tell me how to get the length of a number. I know
    > len(string) will get the length of a string, but it doesn't like
    > len(int). I seem to remember something like %s string. I tried to set
    > a variable = to %s int, but that doesn't work. Is there a function I've
    > forgotten about to convert an integer to a string?
    >
    > Regards
    >
    > Stan


    len('%s'%(1234567,)) gives 7
    len('%s'%(1234**45,)) and len(str(1234**45))
    give 140

    Claudio
     
    Claudio Grondi, Jun 11, 2006
    #7
  8. Felipe Almeida Lessa wrote:

    > Hmmm, you're right.
    >
    > math.floor(math.log(x, 10)) + 1


    x = 0

    </F>
     
    Fredrik Lundh, Jun 11, 2006
    #8
  9. Em Dom, 2006-06-11 às 13:17 -0700, Saketh escreveu:
    > Stan Cook wrote:
    > > Can anyone tell me how to get the length of a number. I
    > > know len(string) will get the length of a string, but it
    > > doesn't like len(int). I seem to remember something like %s
    > > string. I tried to set a variable = to %s int, but that
    > > doesn't work. Is there a function I've forgotten about to
    > > convert an integer to a string?
    > >
    > > Regards
    > >
    > > Stan

    >
    > Use str(int). Then use len(). For example, len(str(12345)) will give
    > you 5.


    $ python2.4 -mtimeit -s 'x=12345' 'len(str(x))' 1000000 loops, best of
    3: 1.33 usec per loop
    $ python2.4 -mtimeit -s 'x=12345;from math import ceil,log' 'ceil(log(x,
    10))'
    1000000 loops, best of 3: 1.54 usec per loop
    $ python2.4 -mtimeit -s 'x=12345**123' 'len(str(x))' 1000 loops, best of
    3: 209 usec per loop
    $ python2.4 -mtimeit -s 'x=12345**123;from math import ceil,log'
    'ceil(log(x, 10))'
    1000000 loops, best of 3: 1.55 usec per loop
    $ python2.4 -mtimeit -s 'x=12345**1234' 'len(str(x))' 100 loops, best of
    3: 19.2 msec per loop
    $ python2.4 -mtimeit -s 'x=12345**1234;from math import ceil,log'
    'ceil(log(x, 10))'
    1000000 loops, best of 3: 1.53 usec per loop


    --
    Felipe.
     
    Felipe Almeida Lessa, Jun 11, 2006
    #9
  10. Em Dom, 2006-06-11 às 22:33 +0200, Sybren Stuvel escreveu:
    > Felipe Almeida Lessa enlightened us with:
    > > To see how many decimal digits it has:
    > >
    > > import math
    > > math.ceil(math.log(i, 10))

    >
    > That doesn't work properly.
    >
    > >>> import math
    > >>> math.ceil(math.log(10000, 10))

    > 4.0
    > >>> math.ceil(math.log(10001, 10))

    > 5.0
    >
    > But "10000" certainly has as much digits as "10001".


    Hmmm, you're right.

    math.floor(math.log(x, 10)) + 1

    --
    Felipe.
     
    Felipe Almeida Lessa, Jun 11, 2006
    #10
  11. In article <>,
    Felipe Almeida Lessa <> wrote:
    .
    .
    .
    >math.floor(math.log(x, 10)) + 1
    >
    >--
    >Felipe.
    >


    .... and you're restricting to the positive integers, I take it?
    I still have rounding problems:
    >>> def l(x):

    ... return math.floor(math.log(x, 10)) + 1
    ...
    >>> l(10)

    2.0
    >>> l(100)

    3.0
    >>> l(1000)

    3.0
    >>> l(10000)

    5.0
    >>> l(100000)

    6.0
    >>> l(1000000)

    6.0
    >>> l(10000000)

    8.0

    While modest mollification improves these results, it's going
    to be tough to beat len(str(x)).
     
    Cameron Laird, Jun 12, 2006
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mitchua
    Replies:
    5
    Views:
    2,806
    Eric J. Roode
    Jul 17, 2003
  2. =?Utf-8?B?SG96aQ==?=
    Replies:
    1
    Views:
    7,004
    Ken Cox [Microsoft MVP]
    Jun 2, 2004
  3. M. Clift
    Replies:
    8
    Views:
    433
    M. Clift
    Aug 19, 2004
  4. Steven
    Replies:
    8
    Views:
    491
    Arndt Jonasson
    Feb 3, 2006
  5. kquirici
    Replies:
    13
    Views:
    1,037
    Rainer Weikusat
    Jun 19, 2012
Loading...

Share This Page