473,503 Members | 7,578 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to get the length of a number

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
Jun 11 '06 #1
9 30792

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.

Jun 11 '06 #2
Stan Cook wrote:
Is there a function I've forgotten about to convert an
integer to a string?


str(value)

</F>

Jun 11 '06 #3
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.

Jun 11 '06 #4
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
Jun 11 '06 #5
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.

Jun 11 '06 #6
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
Jun 11 '06 #7
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.

Jun 11 '06 #8
Felipe Almeida Lessa wrote:
Hmmm, you're right.

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


x = 0

</F>

Jun 11 '06 #9
In article <ma***************************************@python. org>,
Felipe Almeida Lessa <fe**********@gmail.com> wrote:
Jun 12 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
14610
by: Mitchua | last post by:
Simplified a bit, I'm parsing HTML documents to get sentences e.g. my $html = get($URL); # remove all HTML TAGs...blah blah blah @sentences = split(/\./, $html)); then I'm trying to determine the...
15
6225
by: fdunne2 | last post by:
The following C-code implements a simple FIR filter: //realtime filter demo #include <stdio.h> #include <stdlib.h> //function defination float rtFilter1(float *num, float *den, float...
14
5808
by: Luiz Antonio Gomes Pican?o | last post by:
How i can store a variable length data in file ? I want to do it using pure C, without existing databases. I'm thinking to use pages to store data. Anyone has idea for the file format ? I...
1
57821
by: Samuel R. Neff | last post by:
Are there any differences between using Array.Length and Array.GetUpperBound(0) on a one-dimensional array? We have a team of developers and most people use Array.Length but one developer uses...
13
3627
by: coinjo | last post by:
Is there any function to determine the length of an integer string?
10
4560
by: Lyle Fairfield | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaac11/html/acfctNZ_HV05186465.asp "If the value of the variant argument is Null, the Nz function returns the number zero or a...
6
2681
by: foreverbored75 | last post by:
Hello All! I am just learning c++ in school and I have the following question: Is there a way for the user to input the length of an array (console application) without using another variable?...
3
2833
by: spoonybard | last post by:
Hi Everyone, I have an ASP.Net 2.0 C# web application that requires a user to enter in two sets of numbers. The first number is broken into 4 textboxes and the second number is broken into 2...
5
1818
by: colin | last post by:
some enumerable types like collections use Count but but other enumerable types like arrays use length why the difference? Im not sure what my function is going to be passed yet, some...
8
3236
by: Andrew Smallshaw | last post by:
I'm working on a data structure that began life as a skip list derivative, but has evolved to the point that it now only has a passing resemblance to them. Each node of this structure has a few...
1
6976
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5566
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
729
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
372
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.