Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "reading digits from a double" Watch "reading digits from a double" New topic
Author

reading digits from a double

Justin Kuyken
Greenhorn

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 2400....read 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 ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: reading digits from a double
 
Similar Threads
How to avoid exponential in STRING to DOUBLE conversion
static vs non static methods
Recommendation for Collecting double [][] from user input
question about refactoring conditional logic
Several questions about this program