DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2004
    Posts
    9

    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?

    Thanks for any advice :-)

  2. #2
    Join Date
    Oct 2004
    Posts
    311
    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"

  3. #3
    Join Date
    Feb 2004
    Posts
    541
    it would be three or less characters if dividing by 999 results in a number less than one. 1000/999 > 1

  4. #4
    Join Date
    Mar 2004
    Posts
    635
    int x = 999;
    String temp = x + "";

    int length = temp.length();

  5. #5
    Join Date
    Sep 2004
    Posts
    9
    Thanks for all the replies - Phaelax method worked for me :-)

    Cheers all

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links