How to round a variable's value?

Basically, how do I convert "floating" numbers like 6.25, etc into "integer" numbers - (1,2,3...)?
I know there's a Math.round script call, but I'm not sure how it works. The variable has nothing to do with battles.
Sooo, is this a Javascript question? If so, Math.round(arg) is correct.

Code:
const integer = Math.round(6.25);

integer is 6 after this call.
I think you should give an example of where you need that, because by default the variables are integers and cut off any decimal valus - in fact you need to trick the engine into converting them into float before they can store a float number.
so the solution might be much simpler...
So, I have this semi-complex formula that determines the game's difficulty based on your stats.
Then an enemy appears and its speed is determined by this formula.
So for example, if the "DIFFICULTY" variable equals 2.3, it'll activate DIFFICULTY SETTING 3(or 4, etc) since it's bigger than 2, but it should be 2.

Of course, I could do that by making a long Conditional branch that would check difficulty's value by decimals, but I wonder if there's a simpler solution.
1607858587041.png

1607858243415.png
Ah I see the problem. You are using Plugin Commands. It's in the Plugin Dev's hand if they want to support JS Calls in a Command, usually they don't. And a Plugin Command is not a place where you can add a JS snippet, so "Math.round()" will not work.

So your thread title should not be "how to round Variables?" but more like "how to eval JS call in a Plugin Command?".

Fortunatly, any Plugin Call can be written as JS Script Call. Add a JS Script Call and paste this snippet. I'm not sure if it is 100% correct, so please give feedback if it works.

Code:
this.pluginCommand("qSpeed", ["e2", "set", Math.round(3.5), "accel"]);
I think that variables (in the sense of "Variable #1", "Variable #2, etc., not in the sense of variables within your plugin code) are automatically either rounded or floored (rounded down) automatically after any calculation; for example, if you store the result of 12 / 5 in Variable #2, and then you look at the value of Variable #2 using the testplay debug or a Show Message command, it will show 2, not 2.4. I think it's different between each versions but I believe that's how it works in MV.

If you still specifically need to round the variable, you can do it in the event editor by using Control Variables and setting the value to the script line:
Math.round($gameVariables.value(X))
or you can do it with code alone by using the script call:
$gameVariables.setValue(X, Math.round($gameVariables.value(X)))
Replace X with the Variable # that you want to round.
@Aerosys Sorry. Not sure what you were referring to.

@Wavelength Oh, the script totally worked! Yep, actually, I've initially tested that in the Show message command and it would show me unrounded numbers. Now it works fine though.
Thread closed p

Original thread: https://forums.rpgmakerweb.com/threads/how-to-round-a-variables-value.130929/