String to number conversion


Results 1 to 5 of 5

Threaded View

  1. #3
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    970


    Did you find this post helpful? Yes | No

    Default

    PBP does not support floats. So, converting those numbers will be difficult.

    However, if like a challenge, this is a rough draft of how I would try my hand at it. This is not the best code that can be written for this though; just a pointer.

    Code:
    IntResult = 0
    FractionResult = 0
    Exponent = 0
    Units = ""
    WhichPart = 0                   ' 0 = Integer part, 1 = fractional part, 2 = exponent part
    ExpFlag = 0                       ' set to 1 when reading the exponent
    Pointer = points to start of number
    
    while (character@Pointer != end of string)
        select case WhichPart
        case IntPart:              ' whichpart = 0
            IntResult = IntResult * 10
            select case character@pointer
            case "0" to "9":
                   IntResult = IntResult + (character@pointer-'0')
            case "."
                   WhichPart = 1         ' now on, gather fractional part
            end select
        case FractionalPart:      ' whichpart = 1
            FractionResult = FractionResult * 10
            select case character@pointer
            case "0" to "9":
                   FractionResult = FractionResult + (character@pointer-'0')
            case "e" OR "E":
                   WhichPart = 2         ' now on, gather Exponent
            end select
        case ExponentPart:              ' whichpart = 2
            Exponent = Exponent*10
            select case character@pointer
            case "0" to "9":
                   Exponent = Exponent + (character@pointer-'0')
            case else
                   Units = character@pointer
            end select
        end select
    
        ' move to the next character
        increment Pointer by 1
    wend
    This is purely 'pseudo code' and not PBP though it looks a bit alike. At the end of this routine, you should have 4 values - Integer result, Fractional Result, Exponent and Units either V or A. Now, you combine them as you deem fit and have your resulting answer

    Regards
    Last edited by Jerson; - 13th September 2010 at 05:37. Reason: Added some info

Members who have read this thread : 0

You do not have permission to view the list of names.

Tags for this Thread

Posting Permissions

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