String to float convertion

# Thread: String to float convertion – 1010 days old

1. 1

## String to float convertion

Originally Posted by peaps
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```
Originally Posted by Harrison
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```

2. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

The advertisements we display are relevant to this web site and your browsing history

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

Thank you for your attention.

3. 2

## 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```

4. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

The advertisements we display are relevant to this web site and your browsing history

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

Thank you for your attention.

5. 0

## Re: String to float convertion

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

6. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

The advertisements we display are relevant to this web site and your browsing history

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

Thank you for your attention.

##### Users Browsing this Thread

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

#### Posting Permissions

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