Yesterday 20:25
Forum: Proton Plus Compiler v3
Starter: geoff_c
Views: 0
Replies: 1
Yesterday 20:08
Forum: Proton Users File Server
Starter: Les
Views: 0
Replies: 0
Yesterday 19:18
Forum: The Lounge
Starter: Henk57
Views: 0
Replies: 67
Yesterday 14:37
Forum: hadv215's Feedback Forum
Starter: Dompie
Views: 0
Replies: 3
Yesterday 09:30
Forum: The Lounge
Starter: fanie
Views: 3727
Replies: 32
+ Reply to Thread
Results 1 to 3 of 3

Thread: String to float convertion274 days old

  1. #1
    Prolific Poster joesaliba's Avatar
    Join Date
    Sep 2004
    Thumbs Up
    Received: 79
    Given: 20
    Total Downloaded
    2.85 GB

    1 Not allowed!

    Default String to float convertion

    Quote Originally Posted by peaps View Post
    This is my string to float function.

    n is a byte variable. The rest are hungarian notation.

        ' // Set n to be the upper index of the string ie the Least Significant Digit
        Clear fTotal
        n = Len(sTemp) -1
        fMultiplier = 1
        For n = n To 0 Step -1
            bUnit = LookDown sTemp[n], ["0123456789."]
            If bUnit <= 9 Then
                fTotal = fTotal + bUnit * fMultiplier
                fMultiplier = fMultiplier * 10
                fTotal = fTotal / fMultiplier
                fMultiplier = 1
            End If      
    Quote Originally Posted by Harrison View Post

    Has anyone modified this code to also read '-' e.g. -0,123 which is a negative number.

    Looking for a string to float converter I came across this old thread, and have managed to include the `-' sign, as Harrison asked for.

    The old thread was marked as `solved' so I opened a new thread to post my findings. Hopes it can help someone: -

    Dim sTemp   As String * 15  ' Variable used to hold temporary strings
    sTemp = "-1.567"        ' An example for a negative string number
    Sign  = 0                                       ' Clear Sign
    fTemp = 0                                       ' Clear fTemp
    bTemp = Len(sTemp) -1                           ' Take length of string
    fMultiplier = 1                                 ' Load fMultiplier with 1
    For bTemp = bTemp To 0 Step -1                  ' Create a loop length as sTemp
        bUnit = LookDownL sTemp[bTemp] , ["0123456789.-"] ' Load bUnit according to match
        If bUnit = 11 Then                          ' If bUnit = 11, (-) then
            Sign = 1                                ' load Sign with 1 to indicate neg. value
            Continue                                ' jump to Next, skip the following
        EndIf                                       ' End If...Then instruction
        If bUnit < 10 Then                          ' If bUnit is smaller or equal 9 then
            fTemp = fTemp + (bUnit * fMultiplier)   ' add fTemp to bUnit and multiply it
            fMultiplier = fMultiplier * 10          ' increase multiplier
        Else                                        ' else if bUnit is greater 9 then
            fTemp = fTemp / fMultiplier             ' divide fTemp by fMultiplier
            fMultiplier = 1                         ' reload fMultiplier with 1
        End If                                      ' End If...Then instruction
    Next                                            ' Next loop
    If Sign = 1 Then fTemp = -fTemp                 ' Convert to negative if required
    Last edited by joesaliba; 19th August 2017 at 14:39.


  2. #2
    Administrator John Drew's Avatar
    Join Date
    Feb 2002
    Thumbs Up
    Received: 96
    Given: 28
    Total Downloaded
    1.57 GB

    2 Not allowed!

    Default Re: String to float convertion

    Hi Joseph
    Been playing around with other solutions. Here's one that takes a different tack. Just for fun. It makes use of the Val function after removing "-" and "." to enable an integer result - then converts it to a float. Uses a few more variables but compiles with a few hundred less bytes and provides the negative results OK.

    Dim Counter1    As Byte         'loop counter
    Dim bLength     As Byte         'length of example string
    Dim sString     As String * 10  'the test string
    Dim sTempStr    As String * 10  'Variable used to hold temporary strings
    Dim Sign        As Bit          'Recognise a negative if 1
    Dim fResult     As Float        'the final result
    Dim dResult     As Dword        'preliminary result without sign or negative
    Dim DotFound    As Bit          'position of decimal point in original string
    Dim Divisor     As Dword        'power of ten
    Dim NewCount    As Byte         'loop counter for temporary string
    Dim bStart1     As Byte         'starting point for copy (avoids minus sign)
    GoTo Main
        Divisor = 1 :Sign = 0: DotFound = 0:bStart1 = 0: NewCount = 0   'clear the variables
        bLength = Len(sString)                   'length of string
        If sString[0] = "-" Then                 'check for a negative sign
          bStart1 = 1                            'start point to make whole number string
          Sign = 1                               'its a negative
        'now copy the old string into the temporary one without the sign and decimal point
        For Counter1 = bStart1 To blength-1                 ' Create a loop length as sString 
            If sString[Counter1] <> "." Then             
                sTempStr[NewCount]=sString[Counter1]        'copy character if not a "."
                Inc NewCount                                'increment the counter for tempstr
                If DotFound = 1 Then Divisor = Divisor * 10 'working on decimal places
                DotFound = 1                                'a decimal point was found
        dResult = Val(sTempStr,Dec)            'convert sTempStr into a dword
        fResult = dResult/Divisor              'insert the decimal point and convert
        If Sign = 1 Then fResult = -fResult    'adjust the sign
        sString = "-123.4560"                    'test example
        Print At 1,1,sString                     'print the example
        GoSub StringToFloat                      'do the calculation    
        Print At 2,1,Dec6 fResult                'print the result

  3. #3
    Prolific Poster joesaliba's Avatar
    Join Date
    Sep 2004
    Thumbs Up
    Received: 79
    Given: 20
    Total Downloaded
    2.85 GB

    0 Not allowed!

    Default Re: String to float convertion

    Too much busy at the moment John. Will have a look later this week.


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


Similar Threads

  1. Float to String ...?
    By Mentcleer in forum Proton Plus Compiler v3
    Replies: 2
    Last Post: 12th March 2015, 12:02
  2. string to float
    By ADLIN SYSTEMS in forum Wish List / Product Feedback
    Replies: 0
    Last Post: 29th August 2012, 11:57
  3. String to Float
    By fgcity in forum Proton Plus Compiler v3
    Replies: 8
    Last Post: 25th July 2009, 17:14
  4. String to Float Conversion
    By SELCUK in forum Proton Plus Compiler v3
    Replies: 4
    Last Post: 25th March 2008, 15:07
  5. [SOLVED !] Float to String problem
    By Eagleeye in forum Proton Plus Compiler v3
    Replies: 1
    Last Post: 29th August 2007, 07:39

Members who have read this thread since 20th May 2018, 02:02 : 0

Actions :  (Set Date)  (Clear Date)

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