Arcade: finding the modulus?

3439
4
Jump to solution
12-23-2019 01:03 PM
JimBarry
Esri Regular Contributor

Seems Arcade doesn't yet have a math function, like, say:  Mod(value, modulo) -> Number

Anyone have a creative function for calculating one using the existing math functions?

Thanks!

0 Kudos
1 Solution

Accepted Solutions
JimBarry
Esri Regular Contributor

User error.  Arcade doesn't need a Mod() function. It already has a "%" Modulus operator.

View solution in original post

4 Replies
JimBarry
Esri Regular Contributor

User error.  Arcade doesn't need a Mod() function. It already has a "%" Modulus operator.

XanderBakker
Esri Esteemed Contributor

Hi  Jim Barry ,

Nice to see you are doing some Arcade! Normally there are several ways that lead to Rome. Here is an example to play around with. 

var amount = 199.99;
var values = {"Penny": 0.01, "Nickel": 0.05,
                  "Dime": 0.1, "Quarter": 0.25,
                  "One Dollar": 1, "Five Dollars": 5,
                  "Ten Dollars": 10, "Twenty Dollars": 20,
                  "Fifty Dollars": 50, "One Hundred Dollars": 100};

var currencies = Reverse(["Penny", "Nickel", "Dime", "Quarter", 
                  "One Dollar", "Five Dollars", "Ten Dollars", 
                  "Twenty Dollars", "Fifty Dollars", "One Hundred Dollars"]);
var result = "Pay $ " + amount + ":";
for (var i in currencies) {
    var value = values[currencies[i]];
    if (i == Count(currencies)-1){
        var coinsbills = Round(amount / value, 0);
    } else {
        var coinsbills = Floor(amount / value, 0);    
    }
    if (coinsbills > 0) {
        result += TextFormatting.NewLine + "  " + coinsbills + " " + currencies[i];
        // amount -= coinsbills * value;
        amount = Round(amount, 3) % (coinsbills * value);
    }
}

return result;

This will return:

Pay $ 199.99:
  1 One Hundred Dollars
  1 Fifty Dollars
  2 Twenty Dollars
  1 Five Dollars
  4 One Dollar
  3 Quarter
  2 Dime
  4 Penny

As you can see on line 21 and 22 you can use either way. 

Edit: changed code to resolve a nasty error due to rounding. 

JimBarry
Esri Regular Contributor

Hi Xander!!

Oh, that's neat. Thanks for the script. Nice use of modulus, and creative example of doing the same without it.

Here's a slide deck I used, giving an Intro to Arcade 30-min session at the Mid-Atlantic User Conference a few weeks ago: Arcade: an Introduction - Esri MUC 2019 

XanderBakker
Esri Esteemed Contributor

Hi jbarry-esristaff ,

Thanks for sharing the slide deck. Very cool!