Format Specifiers in WPSML

Chris Hector

Logictran, Inc.

Version 1.0

October 19, 2005

Introduction

WPSML algorithmic and plotting capabilities require the ability to control the presentation of computed variables. For example, a variable with a computed value of 1.5 may need to be presented as a whole number (1), a decimal (1.5), currency ($1.50), or a fraction (). The WPSML 2.0 specification described a simple capability to specify this formatting. This document extends that definition and provides more details and examples.

Variable Element Format Attribute

The variable element supports a format attribute. If this attribute is omitted, the system can use a default rendering of the variable. This default rendering is not defined by the specification but should be designed to provide reasonable rendering for any value presented.

The format attribute is a string that specifies the type of output, digits of precision, padding, sign and other formatting characteristics. For example, to specify that variable x should be formatted as an integer, you would write:

variable name="x" format="#">.

# / Integer format.
Expands to as many digits as required to output the integer.
If more than one "#" appears, leading spaces will be used to pad the output to same number of positions.
Negative values are prefixed with a minus sign in the leftmost position of the output.
(#) / Parenthesis around either integer or floating point indicate that negative values are rendered with parenthesis rather than a minus sign.
#.# / Floating point with 1 digits of precision.
Additional digits of precision are added by providing more "#" to the right of the decimal point. Integer portion expands to as many digits as required.
If more than one "#" appears on the left of the decimal, leading spaces will be used to pad the output to same number of positions.
A "#" is not required on the left of the decimal point.
Negative values are prefixed with a minus sign in the leftmost position of the output.
0 / A zero acts the same as "#" except that zero padding is used instead of space padding.
+ / A plus sign forces positive numbers to have a leading plus sign. Negative numbers will have a minus sign in the same position.
#.#E+0 / scientific notation with 1 digits of precision and one digit for the exponent. Additional digits of exponent are added by providing more "0" to the right of the plus.
Additional digits of precision are added by providing more "#" to the right of the decimal point. Integer portion expands to as many digits as required.
If more than one "#" appears on the left of the decimal, leading spaces will be used to pad the output to same number of positions.
A "#" is not required on the left of the decimal point.
Negative values are prefixed with a minus sign in the leftmost position of the output.
# #/# / Mixed fraction with one digit of precision for each of the whole number, numerator and denominator. Additional "#" or "0"'s can be used to adjust padding. The fraction is always reduced to lowest terms.
#/# / Fraction with one digit of precision for the numerator and denominator. Additional "#" or "0"'s can be used to adjust padding. The fraction is always reduced to lowest terms.
$ / A dollar sign forces a currency sign at the start of the number.
, / A comma indicates that the integer portion of the number will be output in groups of three digits with a comma separating the groups.
R# / The R indicates an n'th root (default radix is 2). Note that overriding the radix requires a second input to the formatting process. The second input is provided with the p2 attribute of the variable element.
#R# / A # preceding the R indicates an reduced n'th root (default radix is 2). Note that overriding the radix requires a second input to the formatting process. The second input is provided with the p2 attribute of the variable element.

Examples:

The variables used in these examples have the following values:

Name / Type / Value
v9 / int / 9
v2 / int / 2
v3 / int / 3
vn9 / int / -9
v27 / int / 27

The contents of the variable element show the formatted results given the above values.

R formatting

Number / Example
1 / format_radical(9, 2, FALSE) =
<variable name="v9" format="R#"<math>
<msqrt>
<mrow>
<mn9</mn
</mrow>
</msqrt>
</math</variable>
2 / format_radical(9, 2, TRUE) = 3
<variable name="v9" format="#R#">3</variable>
3 / format_radical(9, 3, FALSE) =
<variable name="v9" format="R#" p2="v3"<math>
<mroot>
<mrow>
<mi>9</mi>
</mrow>
<mrow>
<mi>3</mi>
</mrow>
</mroot>
</math</variable>
4 / format_radical(-9, 2, FALSE) =
<variable name="vn9" format="R#"<math>
<msqrt>
<mrow>
<mn-9</mn
</mrow>
</msqrt>
</math</variable>
5 / format_radical(9, 3, TRUE) =
<variable name="v9" format="#R#" p2="v3"<math>
<mroot>
<mrow>
<mi>9</mi>
</mrow>
<mrow>
<mi>3</mi>
</mrow>
</mroot>
</math</variable>
6 / format_radical(-9, 2, FALSE) =
<variable name="vn9" format="R#"<math>
<msqrt>
<mrow>
<mn-9</mn
</mrow>
</msqrt>
</math</variable>
7 / format_radical(-9, 3, FALSE) =
<variable name="vn9" format="R#" p2="v3"<math>
<mroot>
<mrow>
<mi>-9</mi>
</mrow>
<mrow>
<mi>3</mi>
</mrow>
</mroot>
</math</variable>
8 / format_radical(-9, 3, TRUE) = -
<variable name="v9" format="#R#" p2="v3"<math>-<mrow>
<mroot>
<mrow>
<mi>9</mi>
</mrow>
<mrow>
<mi>3</mi>
</mrow>
</mroot<mrow>
</math</variable>
9 / format_radical(27, 2, FALSE) =
<variable name="v27" format="R#"<math>
<msqrt>
<mrow>
<mn27</mn
</mrow>
</msqrt>
</math</variable>
10 / format_radical(27, 2, TRUE) = 3
<variable name="v27" format="R#"<math<mrow>3
<msqrt>
<mrow>
<mn27</mn
</mrow>
</msqrt> <mrow>
</math</variable>
11 / format_radical(27, 3, FALSE) =
<variable name="v27" format="R#" p2="v3"<math>
<mroot>
<mrow>
<mi>27</mi>
</mrow>
<mrow>
<mi>3</mi>
</mrow>
</mroot>
</math</variable>
12 / format_radical(27, 3, TRUE) = 3
<variable name="v27" format="#R#" p2="v3">3</variable>
13 / format_radical(-9, 2, TRUE) = 3i
<variable name="vn9" format="#R#">3i</variable>
14 / format_radical(-27, 2, FALSE) =
<variable name="vn27" format="R#"<math>
<msqrt>
<mrow>
<mn-27</mn
</mrow>
</msqrt>
</math</variable>
15 / format_radical(-27, 2, TRUE) = 3i
<variable name="vn27" format="#R#"<math<mrow>3i
<msqrt>
<mrow>
<mn-9</mn
</mrow>
</msqrt> </mrow>
</math</variable>
16 / format_radical(-27, 3, FALSE) =
<variable name="vn27" format="R#" p2="v3"<math>
<mroot>
<mrow>
<mi>-27</mi>
</mrow>
<mrow>
<mi>3</mi>
</mrow>
</mroot>
</math</variable>
17 / format_radical(-27, 3, TRUE) = -3
<variable name="vn27" format="#R#" p2="v3">-3</variable>