简体   繁体   English

负数 XSLT 的 Mod 操作

[英]Mod Operation for Negative number XSLT

I am trying to put logic in XSL to get the value.我正在尝试将逻辑放入 XSL 中以获取值。

1+ mod(DateDifference ,number)

DateDifference can be positive or a negative number and formula should work in all scenarios whether date diff is +ve , -ve or 0 . DateDifference可以是正数或负数,无论日期差异是+ve-ve还是0 ,公式都应该适用于所有情况。

This formula in XSL is not working for below values XSL 中的这个公式不适用于以下值

DateDifference= -33
Number=8

The result, I am getting in MS Excel with the same formula is 7 but the XSL result is -1 .结果,我进入 MS Excel ,公式相同为7但 XSL 结果为-1

Can anyone suggest me the correct way or syntax for using the number for all possible DateDifference values?谁能建议我将数字用于所有可能的DateDifference值的正确方法或语法?

Different languages use different definitions for modulo operations.不同的语言对模运算使用不同的定义。 In most programming languages, the mod function is defined as:在大多数编程语言中, mod function 定义为:

n mod d  =  n - d * trunc(n / d) 

(where trunc() means truncation to 0 decimal places) and this is also the algorithm implemented in XSLT. (其中trunc()表示截断到小数点后 0 位),这也是在 XSLT 中实现的算法。

Donald Knuth proposed: Donald Knuth 提议:

n mod d  =  n - d * floor(n / d) 

and this is the algorithm currently implemented in Excel.这是目前在 Excel 中实现的算法。 To get the same result in XSLT, use:要在 XSLT 中获得相同的结果,请使用:

n - d * floor(n div d)

For handling negative and positive mod in XSL用于在 XSL 中处理负模和正模

<xsl:function name="fun:mod" as="xs:string">
        <xsl:param name="int1" as="xs:integer"/>
        <xsl:param name="int2" as="xs:integer"/>
        <xsl:value-of select="($int1 mod $int2 + $int2) mod $int2"/>


    </xsl:function>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM