Difference between revisions of "SPRINTF()"

From Lianjapedia
Jump to: navigation, search
m (Text replace - "Recital" to "Lianja")
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
==Purpose==
 
==Purpose==
 
Function to return a string with 'C' style picture formatting
 
Function to return a string with 'C' style picture formatting
 
  
 
==Syntax==
 
==Syntax==
 
SPRINTF(<expC>, <exp1> [,<exp2>...])
 
SPRINTF(<expC>, <exp1> [,<exp2>...])
 
  
 
==See Also==
 
==See Also==
 
[[ETOS()]], [[FPRINTF()]], [[PRINTF()]], [[TOSTRING()]]
 
[[ETOS()]], [[FPRINTF()]], [[PRINTF()]], [[TOSTRING()]]
 
  
 
==Description==
 
==Description==
 
The SPRINTF() function returns a string including one or more parameters, <exp1> [,<exp2>...], formatted according to the 'C' style options specified in <expC>.  Each formatting sequence is applied to the corresponding parameter in the parameter list from left to right.
 
The SPRINTF() function returns a string including one or more parameters, <exp1> [,<exp2>...], formatted according to the 'C' style options specified in <expC>.  Each formatting sequence is applied to the corresponding parameter in the parameter list from left to right.
  
 
+
{| class="wikitable" width="100%"
{| class="wikitable"
+
!width="20%"|Formatting option||Description
!Formatting option||Description
+
 
|-
 
|-
|%s||Convert to character string (similar to using [[ETOS()]] or [[TOSTRING()]]
+
|valign="top"|%s||Convert to character string (similar to using [[ETOS()]] or [[TOSTRING()]]
 
|-
 
|-
 
|%d||For date parameters
 
|%d||For date parameters
Line 28: Line 24:
 
|%t||For datetime parameters
 
|%t||For datetime parameters
 
|-
 
|-
|%T||For datetime parameters; character day is also displayed
+
|valign="top"|%T||For datetime parameters; character day is also displayed
 
|-
 
|-
 
|%l||For logical parameters: True, False
 
|%l||For logical parameters: True, False
Line 35: Line 31:
 
|-
 
|-
 
|}   
 
|}   
 
  
 
Formatting sequences can also contain the following options.  These are specified in order, between the '%' and the data type letter.
 
Formatting sequences can also contain the following options.  These are specified in order, between the '%' and the data type letter.
  
 
+
{| class="wikitable" width="100%"
{| class="wikitable"
+
!width="20%"|Formatting option||Description
!Formatting option||Description
+
 
|-
 
|-
 
|&#045;||Left-justify
 
|&#045;||Left-justify
Line 47: Line 41:
 
|n||Left pad with spaces to width n
 
|n||Left pad with spaces to width n
 
|-
 
|-
|n.p||Left pad with spaces to width n and include the decimal point and p decimal places (%f only)
+
|valign="top"|n.p||Left pad with spaces to width n and include the decimal point and p decimal places (%f only)
 
|-
 
|-
 
|}
 
|}
 
  
 
==Example==
 
==Example==
Line 117: Line 110:
 
Logicals can also be Yes or No
 
Logicals can also be Yes or No
 
</pre>
 
</pre>
 
  
 
==Products==
 
==Products==
Line 123: Line 115:
 
[[Category:Documentation]]
 
[[Category:Documentation]]
 
[[Category:Functions]]
 
[[Category:Functions]]
[[Category:Expressions and Type Conversion]]
+
[[Category:Formatting Functions]]
[[Category:Expressions and Type Conversion Functions]]
+

Latest revision as of 09:05, 5 February 2013

Purpose

Function to return a string with 'C' style picture formatting

Syntax

SPRINTF(<expC>, <exp1> [,<exp2>...])

See Also

ETOS(), FPRINTF(), PRINTF(), TOSTRING()

Description

The SPRINTF() function returns a string including one or more parameters, <exp1> [,<exp2>...], formatted according to the 'C' style options specified in <expC>. Each formatting sequence is applied to the corresponding parameter in the parameter list from left to right.

Formatting option Description
%s Convert to character string (similar to using ETOS() or TOSTRING()
%d For date parameters
%f For floating point numeric parameters
%y For currency parameters
%t For datetime parameters
%T For datetime parameters; character day is also displayed
%l For logical parameters: True, False
%L For logical parameters: Yes, No

Formatting sequences can also contain the following options. These are specified in order, between the '%' and the data type letter.

Formatting option Description
- Left-justify
n Left pad with spaces to width n
n.p Left pad with spaces to width n and include the decimal point and p decimal places (%f only)

Example

// When %s is specified, the corresponding argument is converted to 
// character format (similar to specifying etos()).
// Widths correspond to the default values, e.g. numerics are 10
cVAR=sprintf('It is %s, %s to be more precise',year(date()),datetime())
echo cVAR
cVAR=sprintf('The value of pi is %s',pi())
echo cVAR
cVAR=sprintf('They cost %s per %s',$99,100)
echo cVAR
cVAR=sprintf('Logicals can be %s or %s',.T.,.F.)
echo cVAR
// Formatting characters can contain a width, which will left pad with spaces 
cVAR=sprintf('Right-justify and pad left: %10s this','Like')
echo cVAR
// Left justify by placing a '-' directly following the '%' character 
cVAR=sprintf('Left-justify and pad right: %-10s this','Like')
echo cVAR
// %d is for numerics
cVAR=sprintf('It is %d',year(date()))
echo cVAR
// %t and %T are for formating datetime data types.
cVAR=sprintf('It is %d, %t to be more precise',year(date()),datetime())
echo cVAR
cVAR=sprintf('It is %d, %T to be even more precise',year(date()),datetime())
echo cVAR
// %f is for floating point numerics
cVAR=sprintf('The value of pi is %f',pi())
echo cVAR
// Decimal places can also be specified for floating point numerics (%f)
cVAR=sprintf('The value of pi to two decimal places is %4.2f',pi())
echo cVAR
// %y is for formatting currency data types
cVAR=sprintf('They cost %y per %d',$99,100)
echo cVAR
cVAR=sprintf('They cost %y per %d',$99,1000)
echo cVAR
cVAR=sprintf('They cost %y per %d',$99,10000)
echo cVAR
//%l and %L are for formatting logical datatypes.
cVAR=sprintf('Logicals can be %l or %l',.T.,.F.)
echo cVAR
cVAR=sprintf('Logicals can also be %L or %L',.T.,.F.)
echo cVAR
It is       2009, 11/11/2009 11:41:51 AM to be more precise
The value of pi is  3.1415926
They cost $99.0000 per        100
Logicals can be True or False
Right-justify and pad left:       Like this
Left-justify and pad right: Like       this
It is 2009
It is 2009, 11/11/2009 11:41:51 AM to be more precise
It is 2009, Wednesday November 11 2009 11:41:51 to be even more precise
The value of pi is 3.141593
The value of pi to two decimal places is 3.14
They cost $99.0000 per 100
They cost $99.0000 per 1000
They cost $99.0000 per 10000
Logicals can be True or False
Logicals can also be Yes or No

Products

Lianja, Lianja Server