FIre Panda
FIre Panda

Reputation: 6637

XSL show result with exponent

While adding numbers using XSL, it's showing result with exponent. For example, if ProductID is 1111

<xsl:value-of select="@ProductID + 1000000"/>

It's showing something like 1.1111E6, however I want the result to be 1001111.

Thanks.

Upvotes: 1

Views: 1513

Answers (2)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243529

You seem to be using an XSLT 2.0 processor (all 5-6 different XSLT 1.0 processors I am using produce the wanted answer -- 1001111.

With an XSLT 2.0 processor, just convert @ProductID to xs:decimal:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xsl:output method="text"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/*">
     <xsl:value-of select="xs:decimal(@ProductID) + 1000000"/>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the following XML document:

<t ProductID="1111"/>

the wanted, correct result is produced:

1001111

Do note:

using the xs:decimal(someNumber) constructor and decimal arithmetic guarantees maximum precision of the result, in case someNumber can have a non-integer value.

The type xs:decimal is a base type for xs:integer, that is any xs:integer is also an xs:decimal, so this solution works also for any integer values.

Upvotes: 1

Michael Kay
Michael Kay

Reputation: 163458

You are presumably using a non-schema-aware transformation, so the value of @ProductID is untyped, which means that when you do arithmetic, the result is an xs:double. If you did integer arithmetic:

xs:integer(@ProductID) + 1000000

the result would be an xs:integer and would be output as such.

In XPath 1.0 all arithmetic is xs:double arithmetic, but the number-to-string conversion rules hide this fact by requiring "whole numbers" to be output as if they were integers.

Upvotes: 1

Related Questions