Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

Modulo operator in macros

November 03 (5 years ago)

Edited November 03 (5 years ago)

Is there a way to use the modulo operation in a macro? I could think about this workaround:

mod10(x) = (x/10 -floor(x/10)) * 10

but that seems really inelegant since computers have the modulo operation built in

November 03 (5 years ago)
GiGs
Pro
Sheet Author
API Scripter

roll20 does indeed support modulo arithmetic. The % symbol can be used, as in: 

x % 10

will give the mod 10 of x.

November 03 (5 years ago)
keithcurtis
Forum Champion
Marketplace Creator
API Scripter

From Math Operators and Functions on the wiki:


%, for modulus division. The result of a % b is the remainder of a / b. If you think back to when you were first learning long division without getting into decimals, you were learning how to perform modulus division. Modulus is useful, for example, to test whether a value is even or odd: a % 2 will be 0 if a is even (and positive) and 1 if a is odd (and positive). In general, the result of a % b when a and b are both whole numbers will be a whole number in the range [0, |b| - 1] where |b| is the absolute value of b. (If a is less than 0, the result will be negative, including -0. -0 is functionally equivalent to 0.)
November 03 (5 years ago)
GiGs
Pro
Sheet Author
API Scripter

wow, that's one way to explain what it means. The wiki excelling as ever.

November 04 (5 years ago)

Wow, I feel really stupid now. For some reason I was convinced that % was a reserved symbol like @ and # are.

Thanks a lot for the replies!