Put an Float in a variable?

Is there a way to save a float value in a Variable?
From the RMMV script calls list:


$gameVariables.setValue(var, value);


You can use this to set whatever value you like: integer, float, string, array; whatever you like.
By default floats are what's used, as that is the basic number format of JavaScript
@taarna23: Thanks for the list 🙂  , But i tried to put a float value and the output was an Integer.


ex: $gameVariables.setValue(1, 5.68);


By printing it on console my expected output is 5.68 but the output was only 5
Ugh. Yup, I just dug in, and this snippet:


Game_Variables.prototype.setValue = function(variableId, value) {
if (variableId > 0 && variableId < $dataSystem.variables.length) {
if (typeof value === 'number') {
value = Math.floor(value);
}
this._data[variableId] = value;
this.onChange();
}
};


...says if the variable being set is a number... truncate it. If it's 1.99, make it 1. If it's 1.0005, make it 1. I actually really don't like this. I wonder why it was done this way.


@Zeriab Sadly, in this case, no floats. Truncate all the things. 🙁
Game_Variables.prototype.setValue = function(variableId, value) {
if (variableId > 0 && variableId < $dataSystem.variables.length) {
if (typeof value === 'number') {
value = Math.floor(value);
}
this._data[variableId] = value;
this.onChange();
}
};




@taarna23: I forgot to check what's the process behind the setValue. Thanks for this one I've created my own function that stored a Float Value in the variable.


Game_Variables.prototype.setValuetoFloat = function(variableId, value) {
if (variableId > 0 && variableId < $dataSystem.variables.length) {
if (typeof value === 'number') {
value = value.toFixed(2);
}
this._data[variableId] = value;
this.onChange();
}
};


For now i there's no error prompt. But i don't know if it's compatible.
That'll work fine.  You could also have done this:


$gameVariables._data[variableId] = value;
$gameVariables.onChange();


That last line is to set the map to refresh, in case any event pages are conditioned by variables.
@taarna23


What you see is an attempt to implement integers on top of floating points. In JavaScript all numbers are double precision floating points.


You can represent integers using floating points I recommend reading http://speakingjs.com/es5/ch11.html#safe_integers if you are interested.
@Zeriab Oh, I see. Ensuring we don't have funky wraparound issues (like World of Warcraft did back in the day with gold, teehee).


Still, it's not really helpful to truncate it. Especially if one WANTS a floating point as in this case. This way, it's limiting all numbers to truncated integers, which seems somewhat unhelpful, as I said.


However, truncate vs. round makes more sense now.

Original thread: https://forums.rpgmakerweb.com/threads/put-an-float-in-a-variable.70196/