FUNCTION "fc_Real2String" : Void
{ S7_Optimized_Access := 'FALSE' }
VERSION : 0.1
VAR_INPUT
Value : Real; // Input value
Digits : Int; // digits before the decimal point
DecPlaces : Int; // decimal places
END_VAR
VAR_OUTPUT
ValueString : String; // value as string
END_VAR
VAR_TEMP
i : Int; // loop variable
j : Int; // loop variable
Temp_Real : Real; // temporary Real-Value
Temp_DInt : DInt; // temporary DInt-Value
Adicity : DInt; // Adicity of the actual position
DigitValue : Int; // value of the actual digit
StringOut : Array [0 .. 12] of Char; // array for output string
StringOut_String AT StringOut : String[10];
StringPos : Int; // actual position
Test : Array [0 .. 12] of Char;
END_VAR
BEGIN
// =============================================================================
//
// convert REAL to STRING
//
// =============================================================================
//
// Input: Value - input value
// Digits - digits defore the decimal point
// DecPlaces - digits after the decimal point
//
// Output: ValueString - Value as string
//
// =============================================================================
// Last change: 2014-04-30
// =============================================================================
//
// 2014-04-30 First version.
//
// =============================================================================
// ===== Limit Value ===========================================================
//
#Temp_Real := #Value;
IF (#Temp_Real >= (10 ** INT_TO_REAL(#Digits - #DecPlaces))) THEN
#Temp_Real := (10 ** INT_TO_REAL(#Digits - #DecPlaces)) - 1.0;
END_IF;
// ===== preinitialize string for output =======================================
//
FOR #i := 1 TO (#Digits + #DecPlaces) DO
#StringOut_String := CONCAT(IN1:=#StringOut_String, IN2:='0');
END_FOR;
IF (#DecPlaces > 0) THEN
#StringOut_String := CONCAT(IN1:=#StringOut_String, IN2:='0');
END_IF;
#j := LEN(IN := #StringOut_String);
// ===== shift decimal point ===================================================
//
FOR #j := 1 TO (#DecPlaces) DO #Temp_Real := #Temp_Real * 10; END_FOR;
#Temp_DInt := ROUND(#Temp_Real);
// ===== convert value =========================================================
//
#StringPos := 2; // In the first two bytes is the length specification
#i := (#Digits + #DecPlaces);
WHILE (#i > 0) DO
// ===== add decimal point ===================================================
IF (#i = #DecPlaces) THEN
#StringOut[#StringPos] := '.';
#StringPos := #StringPos + 1;
END_IF;
// ===== calculate adicity ===================================================
//
#Adicity := 1;
FOR #j := 1 TO (#i - 1) DO #Adicity := #Adicity * 10; END_FOR;
// ===== convert digit =======================================================
//
#DigitValue := DINT_TO_INT(#Temp_DInt / #Adicity);
#StringOut[#StringPos] := INT_TO_CHAR(48 + #DigitValue);
#Temp_DInt := #Temp_DInt - (INT_TO_DINT(#DigitValue) * #Adicity);
#StringPos := #StringPos + 1;
#i := #i -1;
END_WHILE;
// ===== transfer output string ================================================
//
#ValueString := #StringOut_String;
END_FUNCTION