Results 1 to 9 of 9
Thread: finding length on a number
- 12-12-2008, 02:30 AM #1
Member
- Join Date
- Dec 2008
- Posts
- 5
- Rep Power
- 0
- 12-12-2008, 04:38 AM #2
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
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.)
Neil Coffey
Javamex - Java tutorials and performance info
- 12-12-2008, 07:36 AM #3
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
- 12-12-2008, 07:40 AM #4
calculate the log base 10 of it, and there is your answer
- 12-12-2008, 08:10 AM #5
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
@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.Neil Coffey
Javamex - Java tutorials and performance info
- 12-12-2008, 09:09 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Since our thread starter is new for Java, and may be not cleaver with logarithms my suggestion too is go to toString()
- 12-12-2008, 09:25 AM #7
- 12-12-2008, 05:20 PM #8
- 12-12-2008, 08:07 PM #9
Member
- Join Date
- Dec 2008
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
Finding the largest number in an array
By starchildren3317 in forum New To JavaReplies: 14Last Post: 11-03-2010, 06:49 AM -
Finding the highest number
By jigglywiggly in forum New To JavaReplies: 7Last Post: 11-04-2008, 08:14 AM -
how to extract the number of the image which looks like a number
By Crest.Boy in forum Java ServletReplies: 1Last Post: 11-03-2008, 02:38 PM -
Finding the exact length of a string in a JLabel (in pixels)
By Clarkey in forum Advanced JavaReplies: 2Last Post: 03-25-2008, 05:49 AM -
Help with method length
By toby in forum New To JavaReplies: 1Last Post: 07-25-2007, 08:29 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks