3

I found this wonderful piece of ampscript from Adam Spriggs https://gist.github.com/wvpv/478db63dd8b1b0d22b47a4222152226f, and wanted to experiment / test it out to understand it.

%%[

var @day, @suffix

 set @day = Format(Now(),"dd"))
  set @Outputday = Format(Now(),"dd"))

for @day = 1 to 31 do

  /* adapted from http://stackoverflow.com/a/13627586/812377 */
  if mod(@day,10) == 1 and mod(@day,100) != 11 then
    set @suffix = "st"
  elseif mod(@day,10) == 2 and mod(@day,100) != 12 then
    set @suffix = "nd"
  elseif mod(@day,10) == 3 and mod(@day,100) != 13 then
    set @suffix = "rd"
  else
    set @suffix = "th"
  endif

  outputline(concat("<br>",@day,@suffix))

next @i

]%%
<br><br>
%%=v(@Outputday)=%%
<br><br>
%%=Mod(@Outputday, 05)=%%

Focal points here is the output day. For instance, today, on the 15th, it outputs 15. However, what i do not get is the MOD function.

The documentation explains: This function returns the remainder after dividing the first number by the second number.

I've tried three scenarios:

  • %%=Mod(@Outputday, 10)=%% (this shows 5, expected 1.5 as 15/10 = 1.5)
  • %%=Mod(@Outputday, 5)=%% (this shows 0, expected 3 as 15/5 = 3)
  • %%=Mod(@Outputday, 05)=%% (this shows 0, expected 3 as 15/05 = 3)

Could someone help explain this to me? :)

5

Modulo is a core programming concept. It returns the last remaining digit.

%%=Mod(@Outputday, 10)=%% (this shows 5, expected 1.5 as 15/10 = 1.5)

15/10 - Ten goes in 15 1, remainder 5.

%=Mod(@Outputday, 5)=%% (this shows 0, expected 3 as 15/5 = 3)

15/5 - 5 goes in 15 3 times , 0 remainder hence 0 sam with the 3 case

With a value of 25,

%%=Mod(25,10)=%% will still give 5.

For more information please check out the following links:

Wikipedia

Khan Academy

or searching the main stack overflow forum

  • Thanks Data Kid ! :) Is my understanding correct if MOD(28,12) would give 4? and MOD(52,12) would also give 4? (so it divides, and returns remainders that cannot be divided?) – Kenneth Wagner Jun 15 '17 at 7:34
  • You sir are 100% correct :) – Samuel Dare Jun 15 '17 at 7:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.