# Rounding a variable (Solved)

closed

This is probably a dumb question, but I've been trying to solve the problem since yesterday and I've been searching through the old and new forums for it, but I wasn't able to find any solution (or probably I'm just too dumb to find it)

So... here we are again. I'm trying to implement a feature to the game to put a limit on the size of your army:

``````<<set \$charisma = 0>>

<<set \$unemployed = 0>>
<<set \$workers = 0>>
<<set \$knights = 0>>
<<set \$soldiers = 0>>
<<set \$population = \$workers + \$unemployed>>

<<set \$army_size = Math.round(\$workers / 2) + Math.round(\$unemployed / 2) + (\$charisma * 5)>>
``````

As you can see, what I'm trying to do is quite simple. But the problem comes when last variable is displayed on the screen:

"Due to certain existant limitations, the max size of the army is 88.5 troops."

(I'm using a <<print>> macro here to show the variable, but I don't if that's the issue) Anyways, thanks in advance for helping me with this question.

closed with the note: Question answered

+1 vote
by (68.6k points)
selected by

Assuming that's the only place you set \$army_size, then as it's the only arithmetic you do not round in your sample, presumably, \$charisma is not a whole number.

Regardless.  In general, you want to round once, usually at the end, rather than several times.  Try the following:

``<<set \$army_size = Math.round(\$workers / 2 + \$unemployed / 2 + \$charisma * 5)>>``
by (430 points)

Thanks for answering so quickly! But the problem it's still the same. \$charisma is a whole number, and in the game you can actually see that the value of that variable is 1, so I don't know where this  0.5 is coming from.

Image 1

Image 2

by (44.7k points)
If you print the value of "\$army_size" right after the line that sets it above, it should be an integer.

Since that didn't fix it, then you'll need to look through your code to see where else "\$army_size" is getting set.  Also, make sure you're printing "\$army_size" and not "\$Army_size" or some other variable, as upper/lower-case matters.

If you can't find it, then you're going to have to show the code that goes from the point where \$army_size is set to where it's displayed.
by (68.6k points)

If the code I gave above did not solve the issue, then either you have to be modifying \$army_size elsewhere, intentionally or not, or something very strange is going on.  As noted by HiEv, we're probably going to need to see more of the code.

by (430 points)

That's probably what's getting on the way. \$army_size is a variable that appears at the start of the game on the StoryInit passage, but there's a problem with it, because I have to put it every single time I want to make a change to \$army variable

``\$army = \$soldiers + \$kights``

And the reason for doing such a thing it's because \$army isn't a stating value. In fact, the player can train soldiers and knights to make the army bigger and bigger. So I have to <<set>> \$army_size to update it every single time you make a change.

``````<<if \$army_size - \$army >= 1>><<set \$soldiers +=1>>
<<set \$unemployed -=1>>
<<set \$money -=5>>
<<set \$army = \$soldiers + \$knights>>
<<set \$army_size = (\$workers / 2 + \$unemployed / 2 + \$charisma * 5)>>
<<goto "work1">><<else>>You have reached the max size of the army.

[[Siguiente.|work1]]<</if>>``````

And that's what I'm doing all the time...

by (430 points)
Please, don't mind my stupid mispelling errors when it comes to the example we are looking to. The name of the variables it's right in-game (because I'm using spanish names). So, please, don't mind this kind of embarrasing mistakes.
by (68.6k points)
edited

You don't state where you're doing this, but you're not rounding the value in the code you just showed (repeated here).

``<<set \$army_size = (\$workers / 2 + \$unemployed / 2 + \$charisma * 5)>>``

As I noted in my first post, you should be doing something like the following:

``<<set \$army_size = Math.round(\$workers / 2 + \$unemployed / 2 + \$charisma * 5)>>``

If you want its value to be an integer, then you'll have to round the value you assign to it every time if that value can contain a fractional part.

``<<if \$army_size - \$army >= 1>>``
``<<if \$army >= \$army_size>>``