PDA

View Full Version : Formatting output with the printf(), sprintf() and fprintf() functions



barrymavin
2012-10-15, 08:10
Lianja includes the PHP/C-style printf(), sprintf() and fprintf() functions which makes it easy to format HTML tags etc when outputting HTML code into WebViewsWidgets.

printf( format as character [, args...])

character = sprintf( format as character [, args...])

fprintf( fp as numeric, format as character [, args...])

The format can contain any of the following formatting characters:
%s %d %f %y %t %T %l %L

Each formatting character sequence can optionally contain a width and decimal places (only relevant for numeric)

Left justify by placing a '-' directly following the '%' character e.g. %-20s or %-14.2f.

When %s is specified, the corresponding argument is converted to character format (similar to specifying tostring().

%t and %T are for formating datetime data types.
%y is for formatting currency data types
%l and %L is for formatting logical datatypes.



m_date = date()
m_datetime = datetime()
m_number = 10.78
m_string = "Hello world"
? sprintf("%s the date is %s and the datetime is %s and the number is %s", m_string, m_date, m_datetime, m_number)


These functions simplify formatting data. The fprintf() function can be used with fopen(), fclose().