How to get the remainder of 13/5 using KM?

It seems that it's a simple question but I don't know how to fix it after searching the wiki :point_right: manual:Calculations [Keyboard Maestro Wiki]

13/5 = 2 and remainder is 3

How to get '2' and the remainder '3' using some formula in KM.

I don't know much about coding but I'm really want to learn something about it. Many thanks to any replay in advance :wink:

Hi Alice,

I'm far from a math whiz, so offhand I can't think of a way to get both 2 and 3 from a single formula, but I can show you how to do so in two separate ones.

To get 2 from 13/5, you can use the TRUNC() function in a calculation field (make sure to enclose the calculation whose result you want to truncate in parentheses inside the function, otherwise it won't be calculated before truncation):


To get the remainder, 3, you can use the MOD (short for "modulo") operator:

13 MOD 5

You can see the results for yourself in this sample macro, which uses the %Calculate% token to create a calculation field within a plain text field and enable these functions:

Truncate and Modulo Example.kmmacros (2.1 KB)




@gglick Thanks a lot!

1 Like

And if you wanted to write a quotRem function (quotient and remainder) for KM Execute ... Script actions, you could do something like this:


(() => {
    // quotRem :: Int -> Int -> (Int, Int)
    const quotRem = (m, n) => [Math.floor(m / n), m % n];

    return quotRem(13, 5);
    // --> [2, 3]


-- quotRem :: Int -> Int -> (Int, Int)
on quotRem(m, n)
    {m div n, m mod n}
end quotRem

on run
    quotRem(13, 5)
    --> {2, 3}
end run