Formatting Numbers for Output


Formatting Numbers for Output

Formatting numbers becomes especially important for HTML output. XSL Transformations provide several elements and functions that convert numeric formats to readable strings. In this section, we'll cover two of these methods to give you a taste of what's possible: <xsl:decimal-format> and the format-number() function.

<xsl:decimal-format>

The <xsl:decimal-format> function defines the characters and symbols used by the format-number() function when converting numbers to human readable strings, as shown by Listing 2.21.

Listing 2.21 Syntax for the <xsl:decimal-format > Element
 <!-- Category: top-level-element -->  <xsl:decimal-format    name = qname    decimal-separator = char    grouping-separator = char    infinity = string    minus-sign = char    NaN = string    percent = char    per-mille = char    zero-digit = char    digit = char    pattern-separator = char /> 

All the attributes are optional and are defined in Table 2.10.

Table 2.10. Decimal-Format Attribute Definitions

Attribute

Value

Default

Description

name

QName

n/a

The name of the format that, if omitted, specifies the default decimal format.

decimal separator

Character

.

Separates the integral and decimal parts of a number.

grouping separator

Character

,

Separates groups of numbers.

infinity

String

Infinity

String that represents the value infinity.

minus sign

Character

 

Indicates negative numbers.

NaN

String

NaN

String that represents the value NaN (not a number).

percent

Character

%

Indicates percentages.

per-mille

Character

%

Indicates per-mille (per thousand).

zero digit

Character

Indicates where a digit is required.

digit

Character

#

Indicates where a digit might appears unless it's a leading or trailing zero

pattern separator

Character

;

Indicates the separator for positive and negative format patterns.

format-number() Function

The format-number ( number , format , name ) function converts numbers into human readable strings. The <xsl:decimal-format> element utilizes this function to provide the format for the string. This function draws on the formatting specification defined in Java 1.1. For detailed information, that is the place to look.

The three arguments are defined as follows :

  • number . This is the numeric input value.

  • format . This is a format pattern string explained later in this chapter.

  • name . This is the name ( QName ) string of a decimal format created by the <xsl:decimal-format> element. If no named format is present, a default decimal-format is used. A default format is provided by specifying a <xsl:decimal-format> element and not giving it a name.

Although you can use <xsl:decimal-format> to use any character you want for the following values, the default is given. The characters used in specifying the output format are given in Table 2.11. Examples of the usage of different formatting functions are given in Table 2.12.

Table 2.11. Formatting Characters

Character

Default

Description

Zero-digit

A digit always appears at this point.

Digit

#

A digit may appear at this point unless it's a leading or trailing zero.

Decimal point

.

Separates the integral and decimal parts of a number.

Grouping separator

,

Separates groups of numbers.

Minus sign

-

Indicates negative numbers.

Percent sign

%

A number multiplied by 100 and shown as a percent.

Per-mille

%

A number multiplied by 1,000 and shown as per-mille.

Pattern separator

;

Separates the positive and negative format patterns. This enables you to specify separate patterns for each. See the examples.

Apostrophe

'

Escapes special characters in the output so that no translation takes place. To have an output of "**94" use the format pattern '**'00.

E

n/a

Separates mantissa and exponent in scientific notation.

graphics/02icon01.gif

n/a

The universal currency symbol (#xA4), which must be in single quotes (different Java versions handle this differently).

Table 2.12. Formatting Examples

Number

Format Pattern

Result

5678.9

#,#00.00

5,678.90

1000000

#,##0.0#

1,000,000.0

0567

#,##0.0#

567.0

.33

#00%

33%

-4.5

#0.0#;(#0.0#)

(4.5)



XML and SQL Server 2000
XML and SQL Server 2000
ISBN: 0735711127
EAN: 2147483647
Year: 2005
Pages: 104
Authors: John Griffin

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net