Number of characters in an integer

I need to determine the number of characters in an integer. In other words, 1254 would have 4 characters. Sounds really simple, but I'm really slow in figuring out the syntax.
Last edited on
you could use /10 and then check or the value is bigger then 10
Doesn't the integer class specify something like .length or .width? If not, isn't there a simple syntax to cast it as text and then test the length?
Three ways:

1) Use Scipio's idea of repeatedly dividing by 10 until the result is zero, and count
the number of divisions.

2) Stream the integer into a stringstream, get the string out of it, and ask its length.
(careful here in case the integer is negative).

3) log10(x) rounded up gives you the answer immediately.

Which way do you want to go?

Post some non-working code and we can help with the syntax and/or semantics.
I didn't understand Scipio's answer until you explained it a bit more - brilliant!

Now I going to try all of these just to get a handle on this. Thanks!
Topic archived. No new replies allowed.