Jump to content
• Advertisement

Public Group

# Finding the number of digits

This topic is 4810 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

How do I find the number of digits in a number? I'm trying to make my own square root algorithm, and I want to break my number (ex: 123456) into different sections (ex: 100000, 20000, 3000, etc.) so I want to find the number of digits: 6 round the number to that many digits, and go from there. and what should the number be? a string or a number?

#### Share this post

##### Share on other sites
Advertisement
i'd convert to a strong and use a len function. but thats not recomended as it limits your function to the base it was written in.

#### Share this post

##### Share on other sites
Recursive/repeated application of modulo and divide.

#### Share this post

##### Share on other sites
Something like this perhaps:
unsigned digits(unsigned value) { unsigned count, limit = 10; for(count = 1; value >= limit; ++count) {  limit *= 10; } return count;}
But why do you need to number of decimal digits for a square root algorithm?

edit: Oh, I just read the rest of your question. Oluseyi is right, you really want to use / and % or even the infamous div function. But you should *really* consider doing the whole thing in binary instead, it'll be a whole lot easier/faster.

Ok thanks!

#### Share this post

##### Share on other sites
Of course, you might want to look into taking the log base 10 of your number.

#### Share this post

##### Share on other sites
You could just truncate log and add one:

floor(log(13.0)) + 1.0
floor(1.11394335) + 1.0
1.0 + 1.0
2.0

#### Share this post

##### Share on other sites

• Advertisement

### Announcements

• Advertisement

• ### Popular Contributors

1. 1
2. 2
3. 3
Rutin
17
4. 4
khawk
14
5. 5
frob
12
• Advertisement

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633660
• Total Posts
3013218
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!