More on an XQuery format-number function

by Minollo on November 22, 2008

in XML

Recently Chris Wallace has been writing about formatting numbers in XQuery; as you may remember, I did blog about an XQuery-based format-number function some time ago, providing a partial implementation.

Chris mentions my post in his Wikibooks entry, and he has been so nice to run some tests and identify a few bugs and limitations. As I’ve recently had to “enjoy” a red eye flight on my way back to Boston, I couldn’t resist the temptation to fix some of those issues.

Attached is a better version; it includes also support for the typical functionality you would access through xsl:decimal-format. The XQuery includes the tests that Chris posted on the Wikibooks entry, plus a few more, whose results are:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<tests>
 <test>
  <code>local:format-number(12345678.9, '#,###.00')</code>
  <xquery-result>12,345,678.90</xquery-result>
  <xslt-result>12,345,678.90</xslt-result>
 </test>
 <test>
  <code>local:format-number(-12345678.9, '#,###.00')</code>
  <xquery-result>-12,345,678.90</xquery-result>
  <xslt-result>-12,345,678.90</xslt-result>
 </test>
 <test>
  <code>local:format-number(12345.67,'00000000.00')</code>
  <xquery-result>00012345.67</xquery-result>
  <xslt-result>00012345.67</xslt-result>
 </test>
 <test>
  <code>local:format-number(12345.67,'0,000.0000;-000,000.00')</code>
  <xquery-result>12,345.6700</xquery-result>
  <xslt-result>12,345.6700</xslt-result>
 </test>
 <test>
  <code>local:format-number(-12345.67,'0,000.0000;-000,000.00')</code>
  <xquery-result>-012,345.67</xquery-result>
  <xslt-result>-012,345.67</xslt-result>
 </test>
 <test>
  <code>local:format-number(12345.67,',000')</code>
  <xquery-result>12,346</xquery-result>
  <xslt-result>12,346</xslt-result>
 </test>
 <test>
  <code>local:format-number(12345.67,'$,000')</code>
  <xquery-result>$12,346</xquery-result>
  <xslt-result>$12,346</xslt-result>
 </test>
</tests>

This is far from being a complete solution; there are several patterns that are not supported at all; but it’s getting better… As I commented here I do believe that format-number needs to become part of the XQuery language and natively supported by the engine, which is the direction where XQuery 1.1 is moving.

formatnumber-xquery.xq

Bookmark and Share

{ 3 comments… read them below or add one }

1 Monte February 4, 2009 at 6:57 pm

I tested both versions of the format function (formatnumber-xquery.xq and format-number.xquery) using a variety of masks like “#,##0.00″. For a value of -700.00, the result formats like “-,700.00″.

2 Minollo February 4, 2009 at 9:53 pm

Good catch; the updated attached version fixes that problem and adds two tests to check that behavior.

3 chris wallace March 28, 2009 at 4:24 am

Minollo – so sorry I missed this update – in place now in the Wikibook – you should have updated the book yourself :-)

Leave a Comment

Previous post: XQuery for the SQL programmer – And performance?

Next post: Why are you escaping my angle brackets?