Hi,
How do i return the number of digits in an integer? For example, return 5 from 24318, or 7 from 9213214.
Thanx:)
Printable View
Hi,
How do i return the number of digits in an integer? For example, return 5 from 24318, or 7 from 9213214.
Thanx:)
If you mean int which can only have 5 digits you can do an if statement like below. If you mean long or any integer, you can convert the number to a string and get the number of characters.
Code:int digitsInInt(const int &x)
{
if (x < 10) return 1;
if (x < 100) return 2;
if (x < 1000) return 3;
if (x < 10000) return 4;
else return 5;
}
Thanx:)
so simple, i dunno why i didnt think of that:o
If your number is n: int(log10(n)) + 1. This will always return the length of any number regardless of its size. I am not sure if that syntax works in c++, becuase I dont' know what the command is for log base 10, but you get the idea. Hope this helps.
Joe
I was going to post how you could do this with integer division assign, but now i realise how filly has already beaten me in performance :rolleyes: never thought that would happen, congrats filburt :)
this code = slooooowQuote:
Originally posted by filburt1
If you mean int which can only have 5 digits you can do an if statement like below. If you mean long or any integer, you can convert the number to a string and get the number of characters.
Code:int digitsInInt(const int &x)
{
if (x < 10) return 1;
if (x < 100) return 2;
if (x < 1000) return 3;
if (x < 10000) return 4;
else return 5;
}
Nope, filburt has found the simplest and most efficient way of doing it. Yet the MSVC6 compiler isn't really smart to flip the order of the jmp's so you need to modify it a bit:
PHP Code:inline int digitsInInt(const int &x)
{
if (x > 10)
if (x > 100)
if (x > 1000)
if (x > 10000)
return 5;
else return 4;
else return 3;
else return 2;
else return 1;
}
Hmm, that one did jump to a later part in the code which i didn't notice, this should do the trick:
compiles to this:PHP Code:inline int digitsInInt(const int &x)
{
int y;
y=1;
if (x >= 10){
y=2;
if (x >= 100){
y=3;
if (x >= 1000){
y=4;
if (x >= 10000){
y=5;
}
}
}
}
return y;
}
Code:mov ecx, DWORD PTR _a$[esp+4]
mov eax, 1
cmp ecx, 10 ; 0000000aH
jl SHORT $L2271
cmp ecx, 100 ; 00000064H
mov eax, 2
jl SHORT $L2271
cmp ecx, 1000 ; 000003e8H
mov eax, 3
jl SHORT $L2271
cmp ecx, 10000 ; 00002710H
mov eax, 4
jl SHORT $L2271
mov eax, 5
Not only is it not limited to 5 digits numbers, it is one line of code, adn it executes quite quickly. int(log base 10(n) ) + 1. Very simple code that works very well.
Joe
You can put any amount of digits in, just extend.
approximating logaritms are very expensive operations.
If you want an algorithm, you have to use mine. Otherwise, you can use the other method, although I am not partial to writing more lines of code for something that has only a miniscule effect on the system.
Joe
both are algoritms, and you don't have to rewrite an already written algoritm, wheter you advocate functional or object oriented programming.
Hello,
This code should work with any integer.
PHP Code:int x = 10;
int NumberOfDigit = 1;
for(Number;Number>x;x*=10)
{
NumberOfDigit++;
}
If you do not want ot use logs, the last post is right. It is a complete algorithm. The problem with the earlier is more lines of code and it is not complete because it can not handle any number.
Joe
Actually I was inteding to do similar with integer division assign, but i did realize filburt got the simplest and most efficient algoritm:
The largest integer possible is
4 294 967 296
meaning results value can be between 1-10 making an extension of 10 of filburt's algoritm complete.