Results 1 to 9 of 9
  1. #1
    thekrazykid is offline Member
    Join Date
    Dec 2008
    Posts
    5
    Rep Power
    0

    Default finding length on a number

    sorry if this is a dumb question, im very new to java (came from python). is there a way to find the length of an int (number of digits) or do you have to convert it to a string and then find the length of that string as you do in python?

  2. #2
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    Well, you don't have to, but that's an easy way of doing it if you're not very mathematically-inclined. (I'm sure you don't have to in Python either -- it'd be a fairly useless language if you did.)

  3. #3
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    Initialize the counter,divide each time your integer by 10 till your integer gets 0,each time advance your counter by one and then you will know how many digits your number contains

  4. #4
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    calculate the log base 10 of it, and there is your answer

  5. #5
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    @serjant -- that's effectively what Integer.toString() does, but with slight optimisation, so I don't think you lose much by just using the library method.
    @fishtoprecords -- with care, yes ("10" has two digits, but log(10) is 1) -- if the OP isn't familiar with logarithms, it's probably safer to use the toString method.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Since our thread starter is new for Java, and may be not cleaver with logarithms my suggestion too is go to toString()

  7. #7
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    Quote Originally Posted by neilcoffey View Post
    @serjant -- that's effectively what Integer.toString() does, but with slight optimisation, so I don't think you lose much by just using the library method.
    Yes you are absolutely right ,he can use what you have suggested.I wanted just to show how logically it works.(I think I remember that from my C/C++ lessons) ;)

  8. #8
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by neilcoffey View Post
    don't think you lose much by just using the library method.
    I would phrase it the other way. You are nearly always better off using a library rather than your own code. The library will be optimized and improved over time. Your own code will stay stagnant.

  9. #9
    thekrazykid is offline Member
    Join Date
    Dec 2008
    Posts
    5
    Rep Power
    0

    Default

    thanks for the help guys.. i never thought of using log10 i suppose that makes sence though as it just tells you what power of 10 your number is.. i think ill stick with .toString() though

Similar Threads

  1. Finding the largest number in an array
    By starchildren3317 in forum New To Java
    Replies: 14
    Last Post: 11-03-2010, 06:49 AM
  2. Finding the highest number
    By jigglywiggly in forum New To Java
    Replies: 7
    Last Post: 11-04-2008, 08:14 AM
  3. Replies: 1
    Last Post: 11-03-2008, 02:38 PM
  4. Replies: 2
    Last Post: 03-25-2008, 05:49 AM
  5. Help with method length
    By toby in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 08:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •