REAL_TO_STRING

REAL into STRING

The function REAL_TO_STRING converts a value from the data type REAL into a value of the data type STRING[15], which has 7 spaces both before and after the decimal point. The resulting string is right justified within the range '-999999.0000000' to '9999999.0000000'. The plus sign is omitted in the positive range. Leading zeros are filled with empty spaces (e.g. out of -12.0 the STRING '-12.0').

Parameters

Input

Unnamed input (REAL)
Input data type

Output

Unnamed output (STRING)
Conversion resultSTRING[15]

Remarks

  • The function requires approximately 160 steps of program memory. For repeated use you should integrate it into a user function that is only stored once in the memory.

  • When using the data type STRING with small PLCs like FP-e or FP0, make sure that the length of the result string is equal to or greater than the length of the source string.

Example

POU header

All input and output variables used for programming this function have been declared in the POU header. The same POU header is used for all programming languages.

VAR
		input_value: REAL:=-123.4560166;
		result_string: STRING[15]:='';
END_VAR	

The input variable input_value of the data type REAL is intialized by the value -123.4560166. The output variable result_string is of the data type STRING[15]. It can store a maximum of 15 characters. Instead of using the variable input_value, you can enter a constant directly at the function’s input contact in the body.

POU body

The input_value of the data type REAL is converted into STRING[15]. The converted value is written to result_string. When the variable input_value = 123.4560166, result_string shows ' -123.4560165'.

LD body

BODY
    WORKSPACE</p>
        NETWORK_LIST_TYPE := NWTYPELD ;
        ACTIVE_NETWORK := 0 ;
    END_WORKSPACE
    NET_WORK
        NETWORK_TYPE := NWTYPELD ;
        NETWORK_LABEL :=  ;
        NETWORK_TITLE :=  ;
        NETWORK_HEIGHT := 5 ;
        NETWORK_BODY
B(B_F,REAL_TO_STRING!,Instance,7,1,17,3,,?D?C);
B(B_VARIN,,input_value,5,1,7,3,);
B(B_VAROUT,,result_string,17,1,19,3,);
L(1,0,1,5);
        END_NETWORK_BODY
    END_NET_WORK
END_BODY

IL body

Example

POU header

This example illustrates how you create a STRING[7] with 4 positions before and 2 positions after the decimal point out of the data type REAL.

All input and output variables used for programming this function have been declared in the POU header. The same POU header is used for all programming languages.

VAR
		input_value: REAL:=-123.4560166;
			(*example value*)
		result_string: STRING[15]:='';
			(*result: here ' -123.45'*)
	END_VAR

In this example, both an input variable input_value of the data type REAL and an output variable result_string of the data type STRING[7] are declared.

POU body

In carrying out the operation in question, the standard function MID is attached to the function REAL_TO_STRING. MID creates a central sector in the character string from position P (INT value) with L (INT value) characters.

In the example, the INT constant 7 is entered at the L input of MID, which determines the length of the result string. The INT constant 4 at input P determines the position at which the central sector begins. Out of the variable input_value = -123.4560166, the STRING ' -123.4560166' results from the data type conversion. The MID function cuts off the STRING at position 4 and yields the result_string '-123.45'.

LD body

BODY
    WORKSPACE
        NETWORK_LIST_TYPE := NWTYPELD ;
        ACTIVE_NETWORK := 0 ;
    END_WORKSPACE
    NET_WORK
        NETWORK_TYPE := NWTYPELD ;
        NETWORK_LABEL :=  ;
        NETWORK_TITLE :=  ;
        NETWORK_HEIGHT := 6 ;
        NETWORK_BODY
B(B_F,REAL_TO_STRING!,Instance,7,2,17,4,,?D?C);
B(B_VARIN,,input_value,5,2,7,4,);
B(B_VAROUT,,result_string,24,2,26,4,);
B(B_F,MID!,Instance,19,1,24,6,,?DIN?DL?DP?C);
B(B_VARIN,,7,17,3,19,5,);
B(B_VARIN,,4,17,4,19,6,);
L(17,3,19,3);
L(1,0,1,6);
        END_NETWORK_BODY
    END_NET_WORK
END_BODY

IL body

Modified on: 2019-01-24Feedback on this pagePanasonic hotline