home contribute faq download

FunctX XQuery Functions

functx:ordinal-number-en

Reformats a number as an ordinal number, e.g. 1st, 2nd, 3rd.

Google
Webxqueryfunctions.com

Description

The functx:ordinal-number-en function returns the corresponding ordinal number (in English) of $num. For example, if $num is 245, it will return "245th".

The $num argument must have the type xs:integer (or an untyped value that can be cast to xs:integer), or an error is raised.

Arguments and Return Type

NameTypeDescription
$num xs:integer? the number
return value xs:string

XQuery Function Declaration

See XSLT definition.
declare namespace functx = "http://www.functx.com";
declare function functx:ordinal-number-en
  ( $num as xs:integer? )  as xs:string {

   concat(xs:string($num),
         if (matches(xs:string($num),'[04-9]$|1[1-3]$')) then 'th'
         else if (ends-with(xs:string($num),'1')) then 'st'
         else if (ends-with(xs:string($num),'2')) then 'nd'
         else if (ends-with(xs:string($num),'3')) then 'rd'
         else '')
 } ;

Examples

XQuery ExampleResults
functx:ordinal-number-en(1)
1st
functx:ordinal-number-en(12)
12th

History

Published OnLast UpdatedContributor(s)
2006-06-272007-08-08Priscilla Walmsley, Datypic, pwalmsley@datypic.com, http://www.datypic.com
Datypic XQuery Services

Recommended Reading:

XQuery