How can I calculate the absolute value of a number in XSLT?

前端 未结 4 1850
有刺的猬
有刺的猬 2020-12-10 03:57

I have

 

DifferenceInDays has a value that can be a negative or a posi

相关标签:
4条回答
  • 2020-12-10 04:32

    Some of the answers are complicating life way too much for XSLT 1.0 it's actually much simpler. Using the number formatting you can define a structure for Positive and Negative numbers, the default negative is -0 however you can define your own.

    <xsl:value-of select='format-number(DifferenceInDays,"0;0")'/>
    

    The above code will show the absolute value of Difference in Days just by using the provided formatting function.

    0 讨论(0)
  • 2020-12-10 04:47

    In XPath 1.0 use the following expression:

       $vNum*($vNum >=0) - $vNum*($vNum < 0)
    

    In case this expression is embedded in an XSLT (XML) attribute, the < character must be escaped:

       $vNum*($vNum >=0) - $vNum*($vNum &lt; 0)
    

    In XPath 2.0 (XSLT 2.0) use the abs() function.

    0 讨论(0)
  • 2020-12-10 04:49

    This can be achieved using the xpath abs function.

    <xsl:value-of select="abs(DifferenceInDays)"/>
    
    0 讨论(0)
  • 2020-12-10 04:51

    diffInDays * (1 - 2*(diffInDays &lt; 0))

    0 讨论(0)
提交回复
热议问题