0

I am trying to put logic in XSL to get the value.

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.

This formula in XSL is not working for below values

DateDifference= -33
Number=8

The result, I am getting in MS Excel with the same formula is 7 but the XSL result is -1.

Can anyone suggest me the correct way or syntax for using the number for all possible DateDifference values?

Masudur Rahman
  • 1,585
  • 8
  • 16

2 Answers2

2

Different languages use different definitions for modulo operations. In most programming languages, the mod function is defined as:

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.

Donald Knuth proposed:

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

and this is the algorithm currently implemented in Excel. To get the same result in XSLT, use:

n - d * floor(n div d)
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
  • While the answer is correct, I think it is confusing for someone with an Excel background: Excel knows `TRUNC` and `INT` as functions, and `Int` above is `TRUNC`in Excel, whereas `Floor` above corresponds to `INT` in Excel. – Markus Oct 25 '19 at 07:54
  • To quote from the documentation: TRUNC and INT are similar in that both return integers. TRUNC removes the fractional part of the number. INT rounds numbers down to the nearest integer based on the value of the fractional part of the number. INT and TRUNC are different only when using negative numbers: TRUNC(-4.3) returns -4, but INT(-4.3) returns -5 because -5 is the lower number. – Markus Oct 25 '19 at 07:55
  • 1
    @Markus Many things will be confusing for someone with Excel background. Still, I have accepted your suggestion. – michael.hor257k Oct 25 '19 at 08:25
-1

For handling negative and positive mod in 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>