I need to store the 2 places after decimal (currency amount, so need only upto 2 places) in an XSL variable before I do a ceil function.
<xsl:element name="BaseFare"><xsl:value-of select="ceiling(BaseAmount/Amount * (1 - ($promoDisc div 100)))"/></xsl:element>
For eg. if the result of the Amount is 499 and promoDisc = 8%, then the discounted Amount would be 459.08 – I need to store “08” (with the zero) in a variable for use later, while I return the ceiling amount (460) in the output XML. Thought I could just do a string function and read 2 chars after the decimal into a variable rather than do any math?
There are different ways to do this extraction:
I. Using the string representation of the number:
II. Using standard math functions:
This evaluates to the decimal part of any positive number
$x.Use one of the functions:
format-number(),round(),round-half-to-even()(the last function available only XPath 2.0 / XSLT 2.0) to round it to two decimal places.In XSLT 1.0 one way to get from a positive number exactly the digits in the two decimal places after the decimal point (truncate without rounding), is:
Here is a complete example of the described methods:
When this transformation is applied on any XML document (not used), the result is: