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: [cc lang="xquery"]
local:format-number(12345678.9, '#,###.00')
12,345,678.90 12,345,678.90
local:format-number(-12345678.9, '#,###.00')
-12,345,678.90 -12,345,678.90
local:format-number(12345.67,'00000000.00')
00012345.67 00012345.67
local:format-number(12345.67,'0,000.0000;-000,000.00')
12,345.6700 12,345.6700
local:format-number(-12345.67,'0,000.0000;-000,000.00')
-012,345.67 -012,345.67
local:format-number(12345.67,',000')
12,346 12,346
local:format-number(12345.67,'$,000')
$12,346 $12,346
[/cc]
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.
View all posts from Minollo on the Progress blog. Connect with us about all things application development and deployment, data integration and digital business.
Let our experts teach you how to use Sitefinity's best-in-class features to deliver compelling digital experiences.
Learn MoreSubscribe to get all the news, info and tutorials you need to build better business apps and sites