FP_FORMAT_STRING

Write formatted data into a result string

This instruction writes the formatted data specified at Data1 (expandable to Data16) to a formatted string where the format is specified by the control string sFormat.

Input

sFormat (STRING)

Control string with format specifiers defining how the formatted string should be created

  • String variable or character constant (up to 256 characters) containing the definition how the string should be created.

  • The control string consists of the text, format specifier (%d,%e, etc.), line feed code (\n), and tab code (\t).

  • The formatted string can contain up to 4096 characters. If it exceeds 4096 characters, an operation error will occur.

  • You can specify up to 16 digits for a format specifier. If it exceeds 16 digits, an operation error will occur.

  • The maximum number of characters after conversion of 1 data is 32, excluding %s and %S. If it exceeds 32 characters, an operation error will occur.

  • All character strings that are not recognized as a format specifier are treated as text to be formatted.

Data1 (INT, UINT, WORD, DINT, UDINT, DWORD, REAL, LREAL, STRING)

Data to be formatted

  • Variable data to be written to the formatted string.

  • Arrange the variable data in the order specified in the control string.

Output

Result (STRING)

Formatted string with data in ASCII format.

Remarks

  • The input Data1 can be expanded to a maximum of 16 inputs.

  • The number of inputs Data1..DataN must be the same as the number of format specifiers in the input string specified at sFormat.

  • The data type at an input of DataN must correspond to the according format specifier in the sFormat (see table below).

Explanation and examples of control string sFormat

sFormat: e.g. %+12.5d,

Use string data in the format shown below to specify the type, number of characters, and precision of the formatted string. A variety of options (such as inserting a sign or spaces) can also be selected depending on the type of data to be converted.

Position in the example

Description

sFormat

Binary data

Conversion result in ASCII data

Comment

0

zero padding

'%05d'

100

'00100'

when the number of characters is specified, zero padding can be used by adding zero (0) to the control string sFormat at this position.

+

add a plus sign

'%+4d'

100

'+100'

width four characters, decimal value, + sign added

-

left alignment

'%-6d'

100

'100'

width six characters, decimal value, left aligned

(space) add a space instead of plus sign

'%4d'

100

'100'

width four characters, decimal value filled with spaces

#

insert 0x for hexadecimal numbers

'%#4X'

100

'0X64'

width four characters, numerical value in hexadecimal format starting with 0X

append always a decimal point for real number

'%#8.0f'

123.45678

'123.'

width eight characters, floating-point number, four leading spaces

12

width of the ASCII data element (with or without comma)

When the width is smaller than the number of characters required by this value, an operation error occurs. Please refer to "Error flags".

'%012d'

100

'000000000100'

width 12 characters with nine leading zeros

no width:

'%d,'

100

'100,'

no width is specified, comma is appended

precision after decimal point

.5

any digit after decimal point

'%8.3f'

123.45599

'123.456'

width eight characters, three characters precision after decimal point

12.5

specify the total number of characters (n) and the number of characters of precision (m) with [n.m], [n], or [.m]. The number of characters of precision (m) changes according to the type of conversion data.

d , Ld, i , Li, u ,Lu, x , Lx , b, Lb

represents the number of characters in numerical strings.

f, Lf, e, Le, E, LE

represents the number of characters after the decimal point.

g, Lg, G, LG

represents the number of significant figures.

Type of data to be converted

'%06d'

100

'100'

d , i , u , x , X , b: number of characters in numerical strings

f: number of characters after the decimal point.

g: number of significant figures.

d

signed integer ® decimal ASCII

'%6d'

100

'000100'

width six characters, decimal value with three leading zeros

u

unsigned integer ® decimal ASCII

'%10u'

-100

'100'

width 10 characters, seven leading spaces, unsigned integer

x

unsigned integer ® hexadecimal ASCII

'%4x'

16#12A

'12a'

width four characters, hexadecimal number in lower case

X

hexadecimal upper case

'%4X'

16#12A

'12A'

width four characters, hexadecimal number in upper case

b

BCD integer ® hexadecimal ASCII

'%5b'

16#123

'123'

width five characters, BCD data

f

floating point real number ® floating point ASCII

'%-6.2f'

1.2345

'1.23'

width six characters, left aligned, precision two digits after decimal point

e

floating point real number ® exponential notation ASCII

'%9.3e'

1234.5678

'1.235e+03'

width 9 characters, precision 3 digits after decimal point, exponential lower case

E

exponential upper case 1.23E10

'%9.3E'

1234.5678

'1.235E+03'

width 9 characters, precision 3 digits after decimal point, exponential upper case

g

floating point real number ® floating point ASCII or exponential notation ASCII

'%12g'

1234.5678

'1234.57'

width 12 characters, floating-point number

G

floating or exponential upper case

'%#9.3G'

1234

'1.E+03'

width nine characters, precision three characters after decimal point

s

string ®ASCII

'%10s'

abcdef

'abcdef'

string data (for the specified number of characters)

S

string ®ASCII upper case

'%-10S'

abcDEF

'abcDEF'

string data case-sensitive

Conversion data for the control string sFormat

Control string

Binary data before conversion

ASCII data after conversion

Example

'%d' or '%i'

16-bit data (signed integer)

Decimal ASCII data

'%d' , '%5d' , '%+5d"

'%-5d' , '%05d' , '%10.5d' , '%d,' , '% d'

'%Ld' or '%Li'

32-bit data (signed integer)

'%u'

16-bit data (unsigned integer)

'%u' , '%5u' , '%+5u'

'%-5u' , '%05u' , '%10.5u' , '%u,'

'%Lu'

32-bit data (unsigned integer)

'%x'

16-bit data

Hexadecimal ASCII data (forward/reverse direction)

'%x' , '%5x' , '%-5x'

'%05x' , '%10.5x' , '%x,' , '%#x' , '%X'

'%Lx'

32-bit data

'%b'

16-bit BCD data

'%b' , '%5b' , '%-5b'

'%05b' , '%10.5b' , '%b,'

'%Lb'

32-bit BCD data

'%f'

32-bit single-precision real number data

Floating point ASCII data

'%f' , '%5.2f' , '%+5.2f'

'%-5.2f' , '%05.2f' , '%f,' , '%#f' , '% f'

'%Lf'

64-bit double-precision real number data

'%e'

32-bit single-precision real number data

Exponential notation ASCII data

'%e' , '%5.2e' , '%+5.2e'

'%-5.2e' , '%05.2e' ,'%e,'

'%#5.2e' , '% e' , '%E'

'%Le'

64-bit double-precision real number data

'%LE'

64-bit double-precision long real number data

'%#5.2E' , '% E' , '%E'

'%g'

32-bit single-precision real number data

Exponential notation ASCII data or floating point ASCII data (whichever is shorter in the relevant notation)

'%g' , '%5.2g' , '%+5.2g'

'%-5.2g' , '%05.2g' ,'%g," '%#5.2g' , '%G'

'%Lg'

64-bit double-precision real number data

'%LG'

64-bit double-precision long real number data

'%-5.2G' , '%05.2G' ,'%G," '%#5.2G' , '%G'

'%s'

String data

String data (for the specified number of characters)

'%s' , '%5s' , '%-5s' , '%-05s'

Error flags

sys_bIsOperationErrorHold (turns to TRUE and remains TRUE)
  • if there is an error in the control string specified by sFormat.

  • if forward direction (+) is specified in sFormat when the format is decimal.

  • if the converted result exceeds the area.

  • if a width of the ASCII data element is specified which is smaller than the number of characters required by the value

sys_bIsOperationErrorNonHold (turns to TRUE for one scan)
  • if there is an error in the control string specified by sFormat.

  • if forward direction (+) is specified in sFormat when the format is decimal.

  • if the converted result exceeds the area.

  • if a width of the ASCII data element is specified which is smaller than the number of characters required by the value

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
		bFormat: BOOL:=FALSE;
		sFormatString: STRING[50]:='Current Value: %6d [Min: %6d, Max: %6d]';
		sFormattedString: STRING[50]:='';
		iCurrentValue: INT:=815;
		iMin: INT:=-16756;
		iMax: INT:=23456;	END_VAR

LD body

When the variable bStart changes from FALSE to TRUE, the function is carried out.

BODY
    WORKSPACE
        NETWORK_LIST_TYPE := NWTYPELD ;
    END_WORKSPACE
    NET_WORK
        NETWORK_TYPE := NWTYPELD ;
        NETWORK_LABEL :=  ;
        NETWORK_TITLE :=  ;
        NETWORK_HEIGHT := 7 ;
        NETWORK_BODY
B(B_CONTACT,,bFormat,7,1,9,3,);
B(B_F,E_FP_FORMAT_STRING-3!,,25,0,36,7,,?DEN?dsFormat?dData1?dData2?dData3?AENO?c);
B(B_VARIN,,sFormatString,23,2,25,4,);
B(B_VAROUT,,sFormattedString,36,2,38,4,);
B(B_VARIN,,iCurrentValue,23,3,25,5,);
B(B_VARIN,,iMin,23,4,25,6,);
B(B_VARIN,,iMax,23,5,25,7,);
L(1,0,1,7);
L(1,2,7,2);
L(9,2,25,2);
        END_NETWORK_BODY
    END_NET_WORK
END_BODY

ST body

if (bFormat) then
    sFormattedString := FP_FORMAT_STRING(sFormatString,
                                         iCurrentValue, iMin, iMax);
end_if;

Modified on: 2022-10-05Feedback on this pagePanasonic hotline