I am looking for a way to set a minimum number and a maximum number in a formula.
MV How set A minumin and maximun into a formula?
Learning JavascriptHi!
You can try using the clamp method:
This means that the var c will have a value between 5 and 20.
In this case a + b = 25.
But c will be 20.
But put that into a formula, I believe you can put all your formula into (your formula here).clamp(minimum, max)
But don't know for sure how the formula works in the code, but I believe it is evaluated with an eval, so it may work.
You can try using the clamp method:
JavaScript:
/**
* Returns a number whose value is limited to the given range.
*
* @memberof JsExtensions
* @param {number} min - The lower boundary.
* @param {number} max - The upper boundary.
* @returns {number} A number in the range (min, max).
*/
Number.prototype.clamp = function(min, max) {
return Math.min(Math.max(this, min), max);
};
JavaScript:
var a = 10;
var b = 15;
var c = (a+b).clamp(5, 20)
This means that the var c will have a value between 5 and 20.
In this case a + b = 25.
But c will be 20.
var c = 25.clamp(5, 20)But put that into a formula, I believe you can put all your formula into (your formula here).clamp(minimum, max)
But don't know for sure how the formula works in the code, but I believe it is evaluated with an eval, so it may work.
Thank you very much, this was necessary for a skill that I was doing and I had stopped because I did not know how to do this minimum and maximum. Thanks again and I can work on it again.
Original thread: https://forums.rpgmakerweb.com/threads/how-set-a-minumin-and-maximun-into-a-formula.131412/