How to make JS round a decimal to a whole number?

Hey, guys! Found a snippet earlier from @JamesRyan :
Code:
<Custom React Effect>
if (value > 0 && this.isPhysical()){
var heal = value * 0.8;
target.gainHp(heal);
target.startDamagePopup();
target.startAnimation(x); //Change x into an animation ID if you want to display animation when this effect is activated.
value = value * 0.2;}
</Custom React Effect>

From here:
https://forums.rpgmakerweb.com/index.php?threads/absorb-damage-state.82101/

I'm using that, and the damage calculations are now coming with decimals(to clarify: only when the absorbing state is active), can someone show me how to make it round up?

I tried to do
Math.round(value);
But nothing? Any help is valuable, as I'm trying to learn JS
try something like:

value = (parseInt(value*0.2));

at the end of the section... I use that in all my damage formula's as there are several that use decimals to get a more personalized variance to them
try something like:

value = (parseInt(value*0.2));

at the end of the section... I use that in all my damage formula's as there are several that use decimals to get a more personalized variance to them

Code:
<Custom React Effect>
if (value > 0 && this.isPhysical()){
var heal = (parseInt(value * 0.8));
target.gainHp(heal);
target.startDamagePopup();
target.startAnimation(x); 
//Change x into an animation ID if you want to display animation when this effect is activated.
value = (parseInt(value * 0.2));}
</Custom React Effect>

Beautiful. Tested it and it works. I added yours to the above 🙂
Thank you so much!
Glad to assist, and happy game making!
Strictly speaking, there's three functions for rounding: Math.round(number), Math.floor(number), and Math.ceil(number). Round follows normal rounding I believe (above .5 goes up, below goes down), floor always rounds down, and ceil (as in, ceiling) always goes up. These are useful when you're trying to get the numbers to behave a specific way.

So, if I wanted to always round down, I'd do something like:
value = Math.floor(value);

A note on negatives: floor will return a number that is "more negative" (lower) while ceil will return the number that is "less negative" (higher).
The above post would be the correct solution. Use Math.round, Math.floor and Math.ceil for numbers. ParseInt is meant to be used with strings and return a integer. It works in this situation (since javascript tends to convert variables in the correct form for a function), but Math.round would be the correct way to do it (and it does it more efficiently).
All I will say is that when I was working on my formula's the .round method was acting wonky, and trust me, I tried SEVERAL attempts of using the Math functions in different formats and setups.

While it could be the so-called wrong way to do it, based on watching the data when testing it works just as well as the other methods in post above. I have yet to find any kind of problem stemming from using is, so I wouldn't dare to call it wrong or more efficient. It might be wrong for much larger situations, but for a damage calculation its more than good enough.
@Ahuramazda Don't get me wrong, your method will work, it might take sightly more power but barely noticeable. The thing is that there is a correct way to do things, and the reason is because habits stick. It doesn't mean it works here that it will always work. To be honest it only works because it's javascript. Once you move to C++ or C#, you'll instantly get an error for throwing a integer to a function that accepts a string. What happens in javascript is that that function will automatically convert your float var into a string, than run the function and return an integer.

Math.round is not funky at all. It simply rounds a float to the nearest integer (0.5 rounds up). And if you don't like it then fine, we use Math.floor a lot more (since you don't want 99.8% to round up to 100%).
Its been a toooon of versions back in my game, but when I was using the Math functions my formula's would randomly not do anything when play testing (wouldn't even show a number at all, but never crashed with an error). I was never able to figure it out 100%, so I started using parseInt, and after that they never failed in testing.

But when every single attack/skill/spell has a formula like this:
Code:
Game_Battler.prototype.attack = function(a, b) {
    if (a.isActor()) {
        damage  = (((a.atk+Math.pow((a.atk/10), 2)))*(100/(100.00+b.def))+((a.atk-b.def)/2))
        fi = $gameVariables.value( 8)
        wi = $gameVariables.value( 9)
        wa = $gameVariables.value(10)
        ea = $gameVariables.value(11)
        damage *= ((fi+wi+wa+ea)*0.0025)+1.00
        if (a.isStateAffected(40)) { damage *= 1.25 }
        if (a.isStateAffected(47)) { damage *= 0.75 }
        if (b.isStateAffected(46)) { damage *= 0.75 }
        if (b.isStateAffected(54)) { damage *= 1.25 }
        if (damage <= 0) { damage = 1 }
        damage = parseInt(damage*a.pdr)
        console.log(a._name+": Melee ("+(parseInt(damage*0.8))+"-"+(parseInt(damage*1.2)+")"))
        return damage
    }
    else {
        a._mp += a.mmp/10
        damage  = (((a.atk+Math.pow((a.atk/10), 2)))*(100/(100.00+b.def))+((a.atk-b.def)/2))*1.50
        damage *= (b.level*0.05)+1.00
        if (a.isStateAffected(40)) { damage *= 1.25 }
        if (a.isStateAffected(47)) { damage *= 0.75 }
        if (b.isStateAffected(46)) { damage *= 0.75 }
        if (b.isStateAffected(54)) { damage *= 1.25 }
        if (damage <= 0) { damage = 1 }
        damage = parseInt(damage*a.pdr)
        console.log(a.originalName()+": Melee ("+(parseInt(damage*0.8))+"-"+(parseInt(damage*1.2)+")"))
        return damage
    }
}

and that solution was a way to make it work, I was def gonna run with it even if it didn't feel completely right to me.
Weird, I don't know what to say. I never really had issues with the Math.round function, and so are millions of other programmers. Are the Math functions working in all other cases then? Might be a localized problem.
Yeah, nothing else was ever misfiring when I've used them... Like I said, never did figure out exactly what was going on when it was happening. :/

When I'm working on my game if a bug ever does pop up I always squash it as soon as its found, so knowing my luck it really could have been a side effect from a plugin or something else I had done between setting something up wrong or a simple typo in some setting I was using elsewhere.
Well, no point in changing something that's working, but I would use Math.round when I can over parseInt, unless I want to convert a string in a int. But this is actually an interesting case. If we weren't so busy I suppose we would be searching for why that happened, and the answer would probably be something really dumb we overlooked lol.
This is an interesting conversation. You guys need to get back in here and continue this! Lol!

@Doktor_Q, in the above snippet, could you show me how you'd put it in there? I love the idea of rounded down healing, rounded up damage.

Example:
Code:
 var heal = Math.floor(value * 0.25);

Is that correct?
@PhoenixX92
Code:
<Custom React Effect>
if (value > 0 && this.isPhysical()){
var heal = Math.floor(value * 0.8);
target.gainHp(heal);
target.startDamagePopup();
target.startAnimation(x); //Change x into an animation ID if you want to display animation when this effect is activated.
value = Math.floor(value * 0.2);}
</Custom React Effect>
Is how I would write it, and have written similar code in my own projects. I tend to use floor or ceil over round, because I usually don't need the behavior to change around 0.5

EDIT: Looking at the snippet, it looks like (original value) = heal + (final value)? In that case, you can use .ceil on one, and .floor on the other, so the total value stays consistent. If you use .round, and it splits on exactly 0.5, both would round up and you get an extra point.

Original thread: https://forums.rpgmakerweb.com/threads/how-to-make-js-round-a-decimal-to-a-whole-number.107941/