Rounding off a variable - Coding help

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?

1 Like

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.

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

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

Great appreciations to Reaperoa too!

Np happy scripting!

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?

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)
3 Likes

@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

1 Like

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.

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

1 Like