# $numericalDifference$ Function for Dice?

**URL:** <https://forum.vassalengine.org/t/numericaldifference-function-for-dice/77973>\
**Category:** Module Design\
**Created:** [October 1, 2023, 4:48pm UTC](https://forum.vassalengine.org/t/numericaldifference-function-for-dice/77973 "2023-10-01T16:48:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rodzilla68](https://forum.vassalengine.org/user_avatar/forum.vassalengine.org/rodzilla68/32/6844_2.png) [@Rodzilla68](https://forum.vassalengine.org/u/Rodzilla68)\
**Post date:** [October 1, 2023, 4:48pm UTC](https://forum.vassalengine.org/t/numericaldifference-function-for-dice/77973/1 "2023-10-01T16:48:10Z")

</div>

If I create a dice button with two dice, I can use $numericalTotal$ to get the sum. What I want to do is roll two dice and get the difference between them (0-5). Any way to do that?

---

<div class="post-metadata">

**Author:** ![Brent\_Easton](https://forum.vassalengine.org/user_avatar/forum.vassalengine.org/brent_easton/32/10_2.png) [@Brent\_Easton](https://forum.vassalengine.org/u/Brent_Easton)\
**Post date:** [October 2, 2023, 1:21am UTC](https://forum.vassalengine.org/t/numericaldifference-function-for-dice/77973/2 "2023-10-02T01:21:40Z")

</div>

There are 2 ways to do this.

If you just want to report this using the in-built Report Format of the Dice Button, then the individual dice roll results are available as $result1$ and $result2$. However, to do the match to calculate the difference, you need to use BeanShell for the Report format. The following gives you what you want:

`{"result = " + result + ", Diff = " + Math.abs(result1 -result2)}`

If you want to report this outside of the Dice Button, then it is quite a bit trickier as the only thing you have to work with is the comma delimited list of results exported by the \_result global variable. Assuming, the Dice Button is name ‘2d6’, then the following is the equivalent to the Report Format above:

`{"result = " + d6_result + " diff = " + Math.abs(Integer.parseInt(d6_result.substring(0,1)) - Integer.parseInt(d6_result.substring(2,3)))}`

The Integer.parseInt stuff is needed to convert from strings to numbers and is part of the in-built Java support in BeanShell. It’s not something you would be expected to know.

Your query has actually show up a couple of issues in the Audit Trail in the error log, and also the need for a fully documented toNumber() function to make this a bit simpler:

`{"result = " + d6_result + " diff = " + Math.abs(d6_result.substring(0,1).toNumber() - d6_result.substring(2,3).toNumber())}`

Unfortunately, the toNumber() function will need to wait for Vassal 3.8.

Regards.

---

<div class="post-metadata">

**Author:** ![Rodzilla68](https://forum.vassalengine.org/user_avatar/forum.vassalengine.org/rodzilla68/32/6844_2.png) [@Rodzilla68](https://forum.vassalengine.org/u/Rodzilla68)\
**Post date:** [October 2, 2023, 5:18am UTC](https://forum.vassalengine.org/t/numericaldifference-function-for-dice/77973/3 "2023-10-02T05:18:47Z")

</div>

BeanShell to the rescue! Thanks, @Brent_Easton
