Topic: DMD0028

Help File Version: 2.9.4.37

STR2REAL - Convert String to Real


The Convert String to Real (STR2REAL) instruction is used to convert the contents of the specified string from ASCII text to a Real (floating point) value. The string can contain text the traditional floating point form (number with a decimal point: xx.yy), the exponential form (number with E: x.xEy), or simple decimal form (x.xxxEyy).

 

For example, STR2REAL will convert a string that contains the characters "12.5" to the Real value 12.5. Another example, STR2REAL will convert a string that contains the characters "6.02E+23" to the Real value 6.02E+23.

 

 


Parameters:

Note: Use the F9 key to open the Default Element Selection Tool (the Element Picker or the Element Browser) or use the Down-Arrow key (Auto-Complete) on any parameter field to see a complete list of the memory locations that are valid for that parameter of the instruction.

 

String- the String that contains the data to be converted. This can be any of the system-defined Short Strings (SS Block), or system-defined Long Strings (SL Block), or readable String element.

 

Valid input characters are '0' - '9' and the ".", "E", "e", "+", and "-"

 

If an invalid character is found the valid characters up to that point will be converted and the remaining characters ignored. For example, if the string contains the text "1234x5678", the conversion will result in the value 1234.0. See the optional parameter Index where Stopped for information on how to handle this condition.

 

Because we are dealing with floating point numbers, the following three special cases must be considered : if the conversion results is positive or negative infinity, the result will be +infinity or -infinity respectively. If the conversion results in an invalid number, the result will be NotANum (not a number).

 

Numeric Output - specifies where to store the converted Real value. This can be writable numeric location.

 

Index Where Stopped - if enabled, this specifies a location that will contain the offset into the String after valid digits are found where the conversion process stopped. The following examples display the conditions that will cause the conversion to stop and set the Index Where Stopped value.

 

The input string (SS0) contains the valid string "   1   " which has 3 leading spaces that are automatically skipped, Index Where Stopped is set to first space after '1'

 

Input String

Numeric Output

Index Where Stopped

ERR

MSG

"   1   "

1.0

4

""

""

 

 

The input string (SS0) contains the valid string "   -3.14   " which has 3 leading spaces that are automatically skipped, Index Where Stopped is set to first space after '4'

 

Input String

Numeric Output

Index Where Stopped

ERR

MSG

"   -3.14   "

-3.140000

8

""

""

 

 

The input string (SS0) contains the valid string "   +6.02E+23   " which has 3 leading spaces that are automatically skipped, Index Where Stopped is set to first space after '3'

 

Input String

Numeric Output

Index Where Stopped

ERR

MSG

"   +6.02E+23   "

6.020000E+23

12

""

""

 

 

The input string (SS0) contains the valid string "   .   " which has 3 leading spaces that are automatically skipped, and just a decimal point

 

Input String

Numeric Output

Index Where Stopped

ERR

MSG

"   .   "

0.0000000

-1

"STR2REAL input string couldn't be converted to a floating point value."

""

 


Status Display:

 

The Status display will only show as many of the characters of the String as will fit within the borders of the instruction, typically this is about 50 characters.

 


See Also:

GRAY - Gray Code to Integer

 

SCALE - Scale Value

 

SEG - Hex / BCD to 7 Segment Display

 

SWAPB - Swap Bytes

 


STR2REAL - Convert String to Real

 

STR2INT - Convert String to Integer

 


STRCASE - Convert String to UPPER / lower Case

 

STRCLEAR - Clear Strings

 

STRCMP - String Compare


STRCOPY - Copy String

 

STRCOPYR - Copy a Range of Strings

 

STRDELETE - Delete Substring

 

STRFIND - Find within String

 

STRINSERT - Insert Substring

 

STRPRINT - Print to String

 

STRSUB - Get Substring

 

STRTRIM - Trim Whitespace

 

STRTRUNC - Truncate String


 


Related Topics:

String Functions Overview
 

Scripting Language Reference

 


Rung Example: