# turning string numbers into doubles

• 11-10-2010, 08:33 PM
imorio
turning string numbers into doubles
Im trying to lift a number out of a string and turn it into a double. The first part is done, but I'm having some difficulties with the part after the '.'. I go over all chars in a for-loop. When I have checked it is a number and that the '.' has already passed, this piece of code should do the job:

Code:

```prices.set(prices.size()-1,prices.get(prices.size()-1)+(check-'0')/(10*(devision+1))); devision++;```
prices is an arraylist of doubles, that is where I want to store the number. I want to overwrite the last number (the number I am reading) with the old number, including the new digit. check is a char, that is the character I am examining now in the string. When I devide that by 10, 100, 1000, and add that to the previous number, it should have one more digit at the end. However this doesn't happen. 52.0 still remain 52.0 after this code, even if for example char=='5'. The result should be 52.5 if char=='5'.

ps: while typing this I realise that this "(10*(devision+1)" is wrong, it should raise 10 to the power of devision+1.
• 11-10-2010, 08:36 PM
JosAH
Quote:

Originally Posted by imorio
Im trying to lift a number out of a string and turn it into a double. The first part is done, but I'm having some difficulties with the part after the '.'. I go over all chars in a for-loop. When I have checked it is a number and that the '.' has already passed, this piece of code should do the job:

Code:

```prices.set(prices.size()-1,prices.get(prices.size()-1)+(check-'0')/(10*(devision+1))); devision++;```
prices is an arraylist of doubles, that is where I want to store the number. I want to overwrite the last number (the number I am reading) with the old number, including the new digit. check is a char, that is the character I am examining now in the string. When I devide that by 10, 100, 1000, and add that to the previous number, it should have one more digit at the end. However this doesn't happen. 52.0 still remain 52.0 after this code, even if for example char=='5'. The result should be 52.5 if char=='5'.

ps: while typing this I realise that this "(10*(devision+1)" is wrong, it should raise 10 to the power of devision+1.

Note that there also is the method Double.parseDouble( ... ). It'll save you from all those details ...

kind regards,

Jos
• 11-10-2010, 08:45 PM
imorio
I figured there was, but can you use that methode without knowing the length of the number in the String?
• 11-10-2010, 08:51 PM
JosAH
Quote:

Originally Posted by imorio
I figured there was, but can you use that methode without knowing the length of the number in the String?

Sure, as long as the String parameter makes up a valid double type number. If really needed you can use a BigDecimal.

kind regards,

Jos
• 11-10-2010, 10:43 PM
imorio
Problem is that my String is more like this: airnbgaleugraiuhbgr12.75javbjalbvfbva. Parsing is powerless then. But I already found my mistake. I was doing integer devision. That left me with 0 each time.
• 11-10-2010, 11:25 PM
m00nchile
You could still pull the number part of the string into a substring and parse only that bit.