Go to last post By: Dave-S
Today 15:23
Forum: Proton Plus Compiler v3
Starter: Dave-S
Views: 0
Replies: 2
Today 11:42
Forum: Projects discussion
Starter: yvesmazzon
Views: 0
Replies: 1
Today 11:13
Forum: Proton Plus Compiler v3
Starter: Dave-S
Views: 0
Replies: 9
Go to last post By: fanie
Today 00:55
Forum: The Lounge
Starter: fanie
Views: 0
Replies: 10
Today 00:39
Forum: The Lounge
Starter: shantanu@india
Views: 0
Replies: 3
+ Reply to Thread
Results 1 to 3 of 3

Thread: String to float convertion34 days old

  1. #1
    Prolific Poster joesaliba's Avatar
    Join Date
    Sep 2004
    Posts
    2,591
    Thumbs Up
    Received: 55
    Given: 15
    Total Downloaded
    1.55 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.

    Code:
    StringToFloat:
        ' // 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
            Else
                fTotal = fTotal / fMultiplier
                fMultiplier = 1
            End If      
        Next
        Return
    Quote Originally Posted by Harrison View Post
    Hi,

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

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

    Code:
    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.
    Regards

    Joseph

  2. #2
    Administrator John Drew's Avatar
    Join Date
    Feb 2002
    Posts
    2,882
    Thumbs Up
    Received: 73
    Given: 25
    Total Downloaded
    1.37 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.
    John

    Code:
    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
    
    StringToFloat:
        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
        EndIf 
        
        '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
            Else    
                DotFound = 1                                'a decimal point was found
            EndIf        
        Next    
        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
    Return        
            
    Main:            
        Clear
        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
        End

  3. #3
    Prolific Poster joesaliba's Avatar
    Join Date
    Sep 2004
    Posts
    2,591
    Thumbs Up
    Received: 55
    Given: 15
    Total Downloaded
    1.55 GB

    0 Not allowed!

    Default Re: String to float convertion

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

    Joseph

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. 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 15th September 2017, 10:21 : 1

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