Hi there
is there anyway of counting how long an integer is, using something like strlen, since I dont think that works with integers
e.g. 100000000
is 9 characters long
would appreciate any help
This is a discussion on counting how long an integer is within the C Programming forums, part of the General Programming Boards category; Hi there is there anyway of counting how long an integer is, using something like strlen, since I dont think ...
Hi there
is there anyway of counting how long an integer is, using something like strlen, since I dont think that works with integers
e.g. 100000000
is 9 characters long
would appreciate any help
Convert it to a string and use strlen()
By the way, the example number you gave was too big to be a regular int. Make it a long int.
for ints, use itoa(), for longs, ltoa(). The usage is:
itoa(num, string, 10);
where the last parameter is always "10" if you are using the base-10 (normal) number system.
Last edited by Sebastiani; 11-20-2001 at 10:06 AM.
24bbs.cppCode:if( numeric_limits< byte >::digits != bits_per_byte ) error( "program requires bits_per_byte-bit bytes" );
That depends on the o/s. 32 bit o/s's will use 32 bit ints, allowing signed values up to 2147483647.By the way, the example number you gave was too big to be a regular int.
zen
Here's another way. Credit goes to someone who posted this on the old board.
Code:int numlen(int n) { //get the length of a integer this will work for the length of longs also //but this function will not work with floats or doubles because of the decimal point. return (log10(n)+1); }