aspose file tools*
The moose likes Beginning Java and the fly likes how to find digits of the number???? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to find digits of the number????" Watch "how to find digits of the number????" New topic
Author

how to find digits of the number????

Prem Mander
Greenhorn

Joined: Nov 06, 2001
Posts: 16
hi folks
how do i find out the number of digits in a number and what those digits are ???
any help would be greatly appreciated
thank you
prem
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937

Prem Moola wrote:
how do i find out the number of digits in a number and what those digits are ???
u LiVE oNLy ONcE ,sO LiVE it BIG

kEEp DiVIdiNg bY 10.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
If you're using an integral data type, you could make use of repeated division by 10 and the modulus of division by 10.
Another easy strategy would be to turn the number into a String. With a String you can easily ascertain the length and what each character is.
Getting any ideas?
[ March 18, 2003: Message edited by: Dirk Schreckmann ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Prem Mander
Greenhorn

Joined: Nov 06, 2001
Posts: 16
ya i did
was wondering if there is any command or java lib which already had the feature....
(like string tokenizer and stream tokensizer)
thanks for the prompt reply though
i was wondering something like this
just a thought but am working on it
!**
int number 780
int div = (int)Math.floor(Math.log(number)/Math.log(10));
System.out.println(div);
**!
Prem Mander
Greenhorn

Joined: Nov 06, 2001
Posts: 16
hi dave
i wanted to findout the number of instances of 1 between numbers 1 -100 i.e(between 1 and 10 we have 2 instances of number 1i.e 1 and 10)
intially and then later upgarding to finding any number instances specified by the user at runtime
so if i use the division method it would take lot of time for the program to do the intial division and the checking that digit to the value specified by the user
was just trying to get a way around that any help in achieving that would really be appreciated
thank you
prem
Prem Mander
Greenhorn

Joined: Nov 06, 2001
Posts: 16
sorry dick
didnt realise that i misspelled your name as dave
my aplogies
thank you
prem
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Who's Dick?


"I'm not back." - Bill Harding, Twister
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Prem, what solution have you come up with so far?
Doing a little division doesn't take that long. Building Strings and counting characters is very easy, but would take quite a bit more memory. This is the wrong forum to speak much of performance in. If you're curious for the most efficient solution, then we should mosey on over to The Performance Forum.
I wouldn't be surprised if a pattern in the count of ones appears when considering different orders of magnitude (100, 1000, 10000, etc.), but there might not be one.
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937

Who's Dick?

And who is Dave? Is it Dirk and Eugene, respectively? And what is "!**" in the code below, a new JSP tag?

[ March 19, 2003: Message edited by: Eugene Kononov ]
 
Don't get me started about those stupid light bulbs.
 
subject: how to find digits of the number????