Selecting a digit from a double?

Caporegime
Joined
12 Mar 2004
Posts
29,962
Location
England
Is there a simple way I can select a digit from a double and convert it to int?

I created this statement
Code:
Integer.parseInt((Double.toString(priceperlitre)).substring(0, 1))
which works fine, except that when the double is a different length than expected it can select the decimal point and an exception occurs when converting it to an int.
 
Last edited:
Just check that the substring isn't a decimal point?

ie.

if (substring is a decimal point)
{
look at next character (or last character - whatever you want to do)
}
else
{
convert substring to integer
}

Or you could catch the exception...
Or you could convert to a string and just search and remove the decimal point...
Possibilities are endless
 
Last edited:
The problem is I have 3 seven segment displays, they have 3, 4 and 5 digits respectivley. So I will have to write 12 different assignment statements, and if I have to write if statements as well there will be well over 100 lines! I'm trying to think how to shrink the code down as much as possible.
 
I'm not quite sure exactly what you're doing, but personally, I would get the thing working first, no matter how many lines it takes, and only then start looking at which bits of code are repeated all over the place and shrinking it down. Otherwise, you're trying to think how to make it work at all, and how to do it neatly and efficiently at the same time. Some people can do both at once, but I like to see a solution first, no matter how ugly, before I can get a good handle on how to make it better.

It does sound like a compact solution should be possible, since you're doing very similar things all the time, just with varying numbers of digits. But I would still just get it working first.
 
Last edited:
Is there a simple way I can select a digit from a double and convert it to int?

I created this statement
Code:
Integer.parseInt((Double.toString(priceperlitre)).substring(0, 1))
which works fine, except that when the double is a different length than expected it can select the decimal point and an exception occurs when converting it to an int.

Can you give an example of the input / output you want?

Do you just want the integer part of a double? i.e.

27.33 -> 27

Or do you want a specific digit, like the one always before the decimal place?

27.33 -> 7
 
Hi,

You could get a string version of your double and then use a regular expression to capture different parts of it, with these parts then being used to create integers.

For example, you could capture all the digits from the string into an array of matches with each match being a single digit. In this case you could ensure that the regular expression didn't return the decimal point as a match.

Or you could capture the parts before and after the decimal point in different matches, again excluding the decimal point from the matches.

Either of these approaches would reduce the lines of code you'd need and you would be assured that each match was a digit and nothing else.

Jim
 
why so many lines of code? can't you just make a method that outputs the number to the display, and pass it the number and the display number. this will cut down some code.

also you only need a while loop to get the digit from the double.

just do the double.tostring method and then do:

n = 0;
while(n < string.length()){
int i = parseint(string.charat(n));
outputmethod(i, n) // where n is the display number or however you want to work it out
n++;
}

daven
 
Can you give an example of the input / output you want?

Do you just want the integer part of a double? i.e.

27.33 -> 27

Or do you want a specific digit, like the one always before the decimal place?

27.33 -> 7

A specific digit, each display will display one digit of the boolean.


What are you programming in?

Stelly

Java.

This is my code
 
Last edited:
Back
Top Bottom