# How do I determine the length of an integer?

• 11-07-2004, 10:26 AM
ignite
How do I determine the length of an integer?
Hello,

I have an int variable and I want to determine its length (i.e. I want the int to be of 4 characters).

I understand how to determine the length of a String, i.e.

------------------------------------

String text
int temptext = text.length();

------------------------------------

Can this be done similar with an int variable or would I have to convert the int to a String (and then determine its length) and then back to an int?

• 11-07-2004, 12:23 PM
ractoc
You could simply divide by 999 (in case of a four "character" int and check if the result is less then 1, if it is, the original int was 4 or less "characters"
• 11-07-2004, 03:21 PM
mikeBarr81
it would be three or less characters if dividing by 999 results in a number less than one. 1000/999 > 1
• 11-07-2004, 06:23 PM
Phaelax
int x = 999;
String temp = x + "";

int length = temp.length();
• 11-08-2004, 02:45 AM
ignite
Thanks for all the replies - Phaelax method worked for me :-)

Cheers all