<TestSet>
    <testName>Commas in integers</testName>
    <description>
        <p>Purpose: This function adds comma as thousand separators to a string containing an integer.</p>
    </description>
    <author>Dan McCleary</author>
    <prolog>declare namespace fxx ="http://proptax.mdor.state.mn.us/fxx";
        
        
declare function fxx:add-commas-to-positive-integer ( $in as xs:string? ) as xs:string {
       
if (string-length($in) &lt; 4) 
then ($in) 
else (    
(: mod is always 0, 1 or 2.
      If mod is 1 or 2 then it is the range endpoint to get characters for prefix, 
      if mod is zero we need to return 'nnn,' 
 :) 
        let $mod := string-length($in) mod 3  (: prefix is 'n,' 'nn,' or 'nnn,' :) 
        let $prefix :=
              if ($mod = 0) 
              then (concat(substring($in, 1, 3), ',')) 
              else (concat(substring($in, 1, $mod), ',')) 
        (: remainder is all the characters after the prefix :) 
        let $remainder :=
              if ($mod = 0)
               then (substring($in, 4)) 
               else (substring($in, $mod+1)) (: if we have more that 6 digits we have two commas :) 
        return concat($prefix, fxx:add-commas-to-positive-integer(($remainder))) )
 } ;</prolog>
    <test output="text">
        <task>Check on value</task>
        <code>fxx:add-commas-to-positive-integer('123456')</code>
        <expected>123,456</expected>
    </test>
    <test output="text">
        <task>check integers of length 1 to 17</task>
        <code>let $max := "12345678912345678"
for $i in (1 to string-length($max))
return 
      fxx:add-commas-to-positive-integer(substring($max,1,$i))</code>
        <expected>1 12 123 1,234 12,345 123,456 1,234,567 12,345,678 123,456,789 1,234,567,891 12,345,678,912 123,456,789,123 1,234,567,891,234 12,345,678,912,345 123,456,789,123,456 1,234,567,891,234,567 12,345,678,912,345,678</expected>
        <comment>This is the maximum integer number</comment>
    </test>
</TestSet>