File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes reading digits from a double Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "reading digits from a double" Watch "reading digits from a double" New topic

reading digits from a double

Justin Kuyken

Joined: Aug 26, 2004
Posts: 4
I was just wondering how to read say the first digit from a double
for example if my double is the 2 and use that and read the 4 and use that seperatly
Is this even possible?
Its for doing the colour code for resistors if anyone has any other ideas
Thanks Justin
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
You could use Double.toString(yourDouble) and extract numeric characters with String.subString(). Then convert your numeric characters back to a numeric primitive type.

Otherwise you will have to play around dividing by powers of 10 and taking remainders on division by 10.

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
Convert the double to String and read it by single characters.

SCJP<br />SCWCD <br />ICSD(286)<br />MCP 70-216
Chris Allen
Ranch Hand

Joined: Feb 01, 2003
Posts: 127
Why not something like the following:

I had to put extra code in to stop at the decimal place (since you asked to parse a double instead of an int). Is this what you are looking for?

Wow, didn't realize so many people were looking at the same post!
[ October 25, 2004: Message edited by: Chris Allen ]
I agree. Here's the link:
subject: reading digits from a double
It's not a secret anymore!