# Rounding off a variable - Coding help

**URL:** <https://forum.choiceofgames.com/t/rounding-off-a-variable-coding-help/325>\
**Category:** ChoiceScript Help\
**Created:** [January 26, 2012, 8:25am UTC](https://forum.choiceofgames.com/t/rounding-off-a-variable-coding-help/325 "2012-01-26T08:25:19Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![zenfero](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/zenfero/32/198487_2.png) [@zenfero](https://forum.choiceofgames.com/u/zenfero)\
**Post date:** [January 26, 2012, 8:25am UTC](https://forum.choiceofgames.com/t/rounding-off-a-variable-coding-help/325/1 "2012-01-26T08:25:19Z")

</div>

Hi guys,

How do you round off a variable calculation?

This is what I tried, but couldn’t work. leadership and city\_support are a valid and tested variables.

`  
You assign yourself to increase support.

\*temp selfperform  
\*temp roundselfperform  
\*set selfperform (leadership)/6  
\*set roundselfperform round(selfperform)  
\*set city\_support +(roundselfperform)

`

Also tried this version which fails:

`  
You assign yourself to increase support.

\*temp selfperform  
\*set selfperform round((leadership)/6)  
\*set city\_support +(selfperform)

`

This following version works, but without rounding…and when ${selfperform} is used, the result is a decimal number (13.3333)

`  
You assign yourself to increase support.

```
    *temp selfperform
    *set selfperform (leadership)/6
    *set city_support +(selfperform)

```

`

Any ideas?

---

<div class="post-metadata">

**Author:** ![RETowers](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/retowers/32/21477_2.png) [@RETowers](https://forum.choiceofgames.com/u/RETowers)\
**Post date:** [January 26, 2012, 10:52am UTC](https://forum.choiceofgames.com/t/rounding-off-a-variable-coding-help/325/2 "2012-01-26T10:52:37Z")

</div>

Okay, first, rounding is a new addition (added less than a month ago) so I don’t think many of us have really played with it too much. That said, what seems to be the problem? Are you hitting an error? If so, what is the error message (test the game in Internet Explorer for the proper error message). Is it just not rounding? Without knowing what’s actually going wrong, it’s hard to diagnose the error.

As a test, I tried this:

`*set leadership 10 Set Leadership 10: ${leadership} *set leadership /3 Set Leadership /3: ${leadership} *set leadership round(leadership) Set Leadership Round(Leadership): ${leadership}`

and everything seemed to work fine for me. Did you makes sure to reload the page (F5) each time you test the game? Your browser may be holding an old copy in memory and running that instead.

Two tips for checking things yourself: If everything seems to work fine, but the numbers are just wrong at the end, put a series of outputs after each line (like I did above), so that you can see where your numbers suddenly go wrong, and can narrow down the problem. If, on the other hand, everything keeps crashing, and you can’t figure out why, grab another copy of ChoiceScript and rebuild the chunk that may be crashing one line at a time (or at least a minimal number of lines at a time), running the game after each new line (or group of lines) is added, until the game crashes. Trying one of those will usually make it easier to isolate the problem, which makes fixing it a lot easier.

---

<div class="post-metadata">

**Author:** ![Rob](https://avatars.discourse-cdn.com/v4/letter/r/838e76/32.png) [@Rob](https://forum.choiceofgames.com/u/Rob)\
**Post date:** [January 26, 2012, 11:13am UTC](https://forum.choiceofgames.com/t/rounding-off-a-variable-coding-help/325/3 "2012-01-26T11:13:30Z")

</div>

You may have to go to github and update your files,i think I had to do the same to get rounding to work

---

<div class="post-metadata">

**Author:** ![zenfero](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/zenfero/32/198487_2.png) [@zenfero](https://forum.choiceofgames.com/u/zenfero)\
**Post date:** [January 27, 2012, 10:11am UTC](https://forum.choiceofgames.com/t/rounding-off-a-variable-coding-help/325/4 "2012-01-27T10:11:13Z")

</div>

Thanks Rob! I updated the files and it is now working fine!

Great appreciations to Reaperoa too!

---

<div class="post-metadata">

**Author:** ![Rob](https://avatars.discourse-cdn.com/v4/letter/r/838e76/32.png) [@Rob](https://forum.choiceofgames.com/u/Rob)\
**Post date:** [January 27, 2012, 2:01pm UTC](https://forum.choiceofgames.com/t/rounding-off-a-variable-coding-help/325/5 "2012-01-27T14:01:48Z")

</div>

Np happy scripting!

---

<div class="post-metadata">

**Author:** ![J3loodT4lon](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/j3loodt4lon/32/143559_2.png) [@J3loodT4lon](https://forum.choiceofgames.com/u/J3loodT4lon)\
**Post date:** [September 20, 2016, 6:38am UTC](https://forum.choiceofgames.com/t/rounding-off-a-variable-coding-help/325/6 "2016-09-20T06:38:02Z")

</div>

AAAAAAA another necro!!  
Sorry for that lmao

I just discovered the round ‘command’ and I noticed it seems to be rounding my numbers up. Is there a way to round down instead?

---

<div class="post-metadata">

**Author:** ![dfabulich](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/dfabulich/32/198373_2.png) [@dfabulich](https://forum.choiceofgames.com/u/dfabulich)\
**Post date:** [September 20, 2016, 6:50am UTC](https://forum.choiceofgames.com/t/rounding-off-a-variable-coding-help/325/7 "2016-09-20T06:50:52Z")

</div>

It rounds to the nearest integer, rounding 0.5 up to 1. If you always want to round down, you can subtract 0.5 from the number before rounding, like this:

```
*set units round(units - 0.5)

```

---

<div class="post-metadata">

**Author:** ![J3loodT4lon](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/j3loodt4lon/32/143559_2.png) [@J3loodT4lon](https://forum.choiceofgames.com/u/J3loodT4lon)\
**Post date:** [September 20, 2016, 7:29am UTC](https://forum.choiceofgames.com/t/rounding-off-a-variable-coding-help/325/8 "2016-09-20T07:29:49Z")

</div>

@dfabulich

Okay so here is an example of one of the codes I need rounded down.

```
*set ability_str_modifier round((ability_str -10) /2)

```

As per the code, the strength modifier is equal to the strength score -10, then divided by 2.  
Essentially a formula of x=(y-10)/2  
If y=16 then,  
x=(16-10)/2  
Therefor x=3

If y=15 then,  
x=(15-10)/2  
Therefor x=2.5

If I change the formula to:  
x=(y-10)/2-0.5 and y=15 then x=2 and round has nothing to round up.

If I change the formula to:  
x=(y-10)/2-0.5 and y=16 then x=2.5 and round changes that to a 3, giving me the same result it would before the formula change.

Do I have that right? lmao

---

<div class="post-metadata">

**Author:** ![dfabulich](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/dfabulich/32/198373_2.png) [@dfabulich](https://forum.choiceofgames.com/u/dfabulich)\
**Post date:** [September 20, 2016, 4:06pm UTC](https://forum.choiceofgames.com/t/rounding-off-a-variable-coding-help/325/9 "2016-09-20T16:06:07Z")

</div>

Subtract one more from `ability_str` to round down.

```
*set ability_str_modifier round((ability_str -11) /2)

```

Or, equivalently, subtract an additional 0.5 before rounding.

```
*set ability_str_modifier round(((ability_str -10) /2) - 0.5)

```

… but I think it’s cleaner just to subtract an additional point.

---

<div class="post-metadata">

**Author:** ![J3loodT4lon](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/j3loodt4lon/32/143559_2.png) [@J3loodT4lon](https://forum.choiceofgames.com/u/J3loodT4lon)\
**Post date:** [September 20, 2016, 7:18pm UTC](https://forum.choiceofgames.com/t/rounding-off-a-variable-coding-help/325/10 "2016-09-20T19:18:51Z")

</div>

I worked in the -0.5 and so far everything seems to work working perfectly 🙂
