# Explaining AMPscript “MOD” function

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

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? :)

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.