# Decimal-string() function

For financial applications, it is important to be able to round a number at a specified number of digits past the decimal point. Two digits is the obvious one for doing calculations that have a whole number of cents, but some financial applications want a specific number of fractional cents.

The XPath 1.0 round() function only rounds to integers, and the 'number' type is backed by a standard 'double' float, which lacks precision. It often happens that the result of a calculation is of the form 100.33000000002, where clearly the result content that should go into the XML is 100.33.

One could argue that there should exist intelligent UI controls which understand how to present the XML content of 100.33000000002 as 100.33, but we don't necessarily have such intelligent UI controls across all implementations, and more importantly, the underlying data of 100.33000000002 is actually choking up server-tier applications, forcing us to put in place middle tier code to translate to 100.33. Middle tier code should not be required.

We need a function with the following signature:

string decimal-string(number, number)

The first number parameter is the number to be rounded. The second parameter gives the number of digits of precision past the decimal point to use.

NOTE: XPath 2 has an xs:decimal type, so this would not be as useful there. --Erik Bruchez 22:34, 29 August 2011 (UTC)