Given a set of 'numbers' 0242, 0980, 0526, 1732, ...
How can I transform them to look like 24,2 98,0 52,6 173,2 ?
I'm trying xsl:value-of select="format_number(0242,'#,##'), but that would produce 2,42. With the '##,#' the output is 2,4,2
Cheers.
First, define a custom decimal format and place it at the top level of your stylesheet:
<xsl:decimal-format decimal-separator="," grouping-separator="."/>
Then you can use:
<xsl:value-of select="format-number(0242 div 10, '#,0')"/>
to get 24,2 and so on.
Alternatively, you could use:
<xsl:value-of select="translate(format-number(0242 div 10, '#.0'), '.', ',')"/>
You can divide it by 10 to get the number right, then format it.
<xsl:value-of select="format-number(node() div 10, '#.0')" />
Please note that I used a dot instead of the comma to use the decimal point operator (for operators see definition on w3schools).
Related
I am trying to pad an output in XSLT with variables being multiplied in XSLT 1.0. I cannot use the format-number because it will auto-round the numbers but I need to get the decimals in place. I can get this to work when I use column formatting within the XML, but since this has multiple column formats within the value-of-select, it does not seem to work here and obey the 7 in the code below:
<xsl:value-of select="substring(E_BaseRate * E_NormalHours * 2, 1, 7)" disable-output-escaping="yes"/>
The output I am getting is: 1601.93 translated to 160193 but I need to get the result of 0160193. Is there a way to do this?
I broke this out to give you an idea. You could use as is or put it all together into one select...
<xsl:variable name="numToString" select="string(160193)"/>
<xsl:variable name="numLen" select="string-length($numToString)"/>
<xsl:variable name="value" select="concat(substring('0000000', 1, 7 - $numLen), $numToString)"/>
I want to display a price for India, like this:
5,55,555
And not
555,555
There should be no decimal. There should be a comma, like this:
1,000 one thousand
10,000 ten thousand
1,00,000 one lakh
My code:
<Price>555555</Price>
<xsl:decimal-format name="Format_INR" grouping-separator="," />
<xsl:value-of select="format-number(Price, '#,##,###', 'Format_INR')" />
But it displays
555,555
What did I do wrong?
Thank you for your help.
The XSLT 2.0 specification of format-number() allows irregular grouping separators as in your example.
The XSLT 1.0 specification is based on the Java specification of DecimalFormat, which requires regular intervals between grouping separators.
(To be more precise: the JDK 7 spec requires regular intervals, or at any rate, it treats the last interval as the one to be used: (the interval between the last one and the end of the integer is the one that is used. So "#,##,###,####" == "######,####" == "##,####,####". But the XSLT 1.0 spec refers specifically to JDK 1.1.8, which is pretty-well unobtainable nowadays; my recollection is that it was very vague on such questions, and later versions of the JDK specification essentially documented the bugs in the initial implementation. To the extent that JDK 1.1.8 was vague, XSLT 1.0 implementations are free to do their own thing.)
As already mentioned, in XSLT 2.0 you can use:
<xsl:value-of select="format-number(Price, '#,##,###')" />
This will accommodate numbers up to 9,999,999. Above that, you need to add more separators, e.g.:
<xsl:value-of select="format-number(Price, '##,##,##,###')" />
will work for numbers up to 999,999,999 and so on.
In XSLT 1.0 you can do:
<xsl:choose>
<xsl:when test="Price >= 1000">
<xsl:value-of select="format-number(floor(Price div 1000), '#,##')" />
<xsl:text>,</xsl:text>
<xsl:value-of select="format-number(Price mod 1000, '000')" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="format-number(Price, '#,###')" />
</xsl:otherwise>
</xsl:choose>
This will work for any magnitude of Price. If you need to reuse this, consider making it a named template.
Note that neither method requires you to define a xsl:decimal-format.
I have a filed value like "+000002030" need to convert it to "20.30" how can i do this using XSLT. This value "+000002030" is the dynamic one any value it can come.Please let me know how we can convert it.
In XSLT 1.0, use:
<xsl:value-of select="format-number(translate(value, '+', '') div 100, '#.00')"/>
To make this future-proof, use:
<xsl:value-of select="format-number(number(translate(value, '+', '')) div 100, '#.00')"/>
This will work the same in both XSLT 1.0 and XSLT 2.0.
A solution in XSLT-2.0 would be
<xsl:value-of select="format-number(number('+000002030') div 100, '#.00')"/>
I'm new to xsl and have a problem. I always get "NaN" when i try to do the following:
<xsl:variable name="amount" select="format-number(ARTICLE_PRICE/PRICE_LINE_AMOUNT, '0,00')" />
<xsl:variable name="quantity" select="format-number($quantity, '0,0#')"/>
<xsl:value-of select="format-number($amount * $quantity, '0,00')" />
Can someone tell me what i am doing wrong?
One possible reason out of several:
The result of format-number() is a string; if the string cannot be converted back to a number - for example, if it contains a comma - then you cannot multiply by it.
Well, if i do it like this:
<xsl:variable name="quantity" select="format-number($quantity, '0,0#')"/>
<xsl:value-of select="format-number(ARTICLE_PRICE/PRICE_LINE_AMOUNT * $quantity, '0,00')" />
It works. Still curious why it didn't work if i set the variable "amount"..
The 'NaN' value means you try to use format-number() on a value that is empty or not a valid number.
You have to test the value before using them as number, and formatting them before multiplying them is useless (if you don't use them elsewhere)
Given the XML:
<current rate="0.1412" />
The desired output to screen is:
14.1200%
However, the current transform:
<xsl:value-of select="format-number(current/#rate, '###,##0.0000')"/>%
Yields the output:
0.1412%
Is it possible to do a format-number transform that will output a value in the desired format (four decimal places) given the current XML input or does it require a change in the xml input to better match the expected output?
Say:
<current rate="0.141200" />
There is no need to multiply or use any other tricks - just tell the format-number() function you want the number to be formatted as percent:
<xsl:value-of select="format-number(current/#rate, '#,##0.0000%')"/>
If your rate is being stored as a decimal, then you will need to multiple by 100 to show it as a percentage:
<xsl:value-of select="format-number(current/#rate * 100, '###,##0.0000')"/>%