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

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

Examples

Expression
Result
round-half-to-even(1.1742, 2)
1.17
round-half-to-even(1.175, 2)
1.18
round-half-to-even(2.5, 0)
2.0
round-half-to-even(273, -1)
270
round-half-to-even(-8500, -3)
-8000

Usage

Most of us were probably taught at school that when numbers are rounded, 0.5 should be rounded upward. Professional accountants and statisticians, however, often prefer the “half-to-even” rule because it avoids creating bias: it means that on average, the total of a large set of numbers will remain roughly the same when all the numbers are rounded.

This function is useful when you want to display the results of a numerical calculation to a certain number of decimal places. Floating-point arithmetic often produces rounding errors because decimal values cannot be represented exactly in binary; for example, the result of
0.3e0 div 3
is
0.09999999999999999
rather than
0.1
. Rounding the result say to six decimal places by writing
round-half-to-even(0.3e0div 3,6)
corrects this error, and produces the result
0.1
.

In XSLT, you can also achieve this rounding by using the
format-number()
function, described on page 788.

See Also

ceiling()
on page 723

floor()
on page 779

format-number()
on page 788

round()
on page 870

seconds-from-dateTime, seconds-from-time

The two functions
seconds-from-dateTime()
and
seconds-from-time()
extract the seconds component (including fractional seconds) from an
xs:dateTime
or
xs:time
value.

Signature

Argument
Type
Meaning
input
xs:time?
or
xs:dateTime?
The value from which the seconds component is to be extracted. The type of the supplied argument must correspond to the type implied by the function name. If an empty sequence is supplied, an empty sequence is returned.
Result
xs:decimal?
The seconds component, in the range 0 to 59.999 …

Effect

The function returns the seconds component of the supplied
xs:time
or
xs:dateTime
. The value is from the time as expressed in its local timezone (not normalized to UTC).

Examples

Expression
Result
seconds-from-time(xs:time(“12:35:03.142”))
3.142
seconds-from-dateTime(xs:dateTime(“2008-02-28T13:55:30-01:00”))
30

See Also

current-date()
,
-dateTime()
,
-time()
on page 738

format-date()
,
-dateTime()
,
-time()
on page 781

hours-from-dateTime()
,
-time()
on page 800

minutes-from-dateTime()
,
-time()
on page 832

timezone-from-dateTime()
,
-time()
on page 893

seconds-from-duration

This function extracts the value of the seconds component (including fractional seconds) from a normalized
xs:duration
value.

Signature

Argument
Type
Meaning
input
xs:duration?
The value whose seconds component is to be extracted. If an empty sequence is supplied, an empty sequence is returned.
Result
xs:decimal?
The seconds component, including any fractional seconds
.

Effect

The function returns the seconds component of the supplied
xs:duration
. The duration value is first normalized so that the number of hours is less than 24, the number of minutes is less than 60, and so on. However, months are never converted to days, or vice-versa. The result will be negative if the duration is negative. The result is therefore a decimal number in the range −60.0 to +60.0, exclusive.

Other books

The Survivors Club by Lisa Gardner
025 Rich and Dangerous by Carolyn Keene
The Wedding by Julie Garwood
The Invitation by Samantha Hyde
Pravda by Edward Docx
Night's Master by Amanda Ashley
Apocalypsis 1.07 Vision by Giordano, Mario
Captain of My Heart by Harmon, Danelle
Brotherband 3: The Hunters by Flanagan, John