Results 1 to 6 of 6
  1. #1
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default 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:

    Java 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.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by imorio View Post
    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:

    Java 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

  3. #3
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

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

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by imorio View Post
    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

  5. #5
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    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.

  6. #6
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    You could still pull the number part of the string into a substring and parse only that bit.
    Ever seen a dog chase its tail? Now that's an infinite loop.

Similar Threads

  1. Problems when multiplying numbers from a string
    By davetheant in forum New To Java
    Replies: 5
    Last Post: 09-06-2010, 03:42 AM
  2. add all the numbers in a string
    By gibson.nathan in forum New To Java
    Replies: 29
    Last Post: 09-30-2009, 06:14 PM
  3. parsing numbers in a string
    By rsoler in forum Advanced Java
    Replies: 4
    Last Post: 03-31-2009, 06:05 AM
  4. Turning numbers into asterisks.
    By BugginVT in forum New To Java
    Replies: 7
    Last Post: 02-25-2009, 07:38 AM
  5. validating a string for numbers and letters?
    By lockmac in forum New To Java
    Replies: 1
    Last Post: 08-09-2007, 09:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •