XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition (570 page)

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
3.23Mb size Format: txt, pdf, ePub

, while 0.875 is shown as
0.88
. This is different from the rounding rule that many of us were taught at school, which always rounds 0.5 upward, but it is preferred by many statisticians and accountants because it means that numbers are equally likely to be rounded up or down, which avoids introducing bias. If you prefer a different rounding rule, you can always round the number yourself before formatting it.

If no digits are displayed to the right of the decimal point, then the decimal point itself will not be displayed.

In the integer part of the picture, that is, on the left of the decimal point if there is one, the rules are different, because the value will never be truncated: if you don't define enough digit positions to accommodate the number, the system will display the full integer part of the value anyway. There will always be at least as many digits as there are
0
digits in the picture, but it makes little difference how many
#
signs appear to the left of the decimal separator: their only real function is to space out any grouping separator characters.

The
format-number()
function doesn't provide any direct way of space-padding the number so that numbers can be vertically aligned in a column. With HTML output this is best achieved using the CSS property
text-align:right
. With text output, the simplest way to achieve this effect is to format the number, use the
string-length()
function to determine the length of the result, and then add the requisite number of spaces to the front.

The grouping separator (
,
by default) is commonly used for thousands, but in some countries for ten thousands. You can specify grouping separators either at regular intervals or at irregular intervals, and they can appear in either the integer part or the fractional part of the number. For example, if you write
#,##0.00
, then you will get a grouping separator every three digits to the left of the decimal point, while if you specify
#,##,###,###0.00
, then you will get separators at the specified positions only. The rule is that if all your explicit grouping separators are regularly spaced, then the system will add implicit grouping separators when it extends the picture on the left, but if the explicit separators are at irregular intervals, then no implicit separators will be added.

Other books

The Snow Falcon by Stuart Harrison
The Spinoza Problem by Irvin D. Yalom
The Traitor's Tale by Margaret Frazer
Under His Sway by Masten, Erika
Night Driving by Lori Wilde
It Started with a Scandal by Julie Anne Long