This week's book giveaway is in the Other Languages forum.We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# how to find digits of the number????

Prem Mander
Greenhorn
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
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
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 ]

Prem Mander
Greenhorn
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
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
Posts: 16
sorry dick
didnt realise that i misspelled your name as dave
my aplogies
thank you
prem

Jim Yingst
Wanderer
Sheriff
Posts: 18671
Who's Dick?

Dirk Schreckmann
Sheriff
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
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 ]