Percentage With javascript

0

Good afternoon, I'm in a doubt beast and I need your help. I have a situation where I need to display if the value is 25% more than the current value. But I'm having trouble putting this logic together. I gave a searched but I did not find something that clarifies to me:

var valor_input = 25;
var valorAtual = 50;
var result = DUVIDA;
alert(result);
    
asked by anonymous 09.09.2015 / 23:30

4 answers

4

You can check by dividing by 4 or by multiplying by 0.25

var valor_input = 25;
var valorAtual = 50;

//Dividindo por 4
if( valor_input > (valorAtual/4) ){
    //Aqui você poem o aviso ou um alert
    alert('Maior que 25%');
}

//Multiplicando por 0.25
if( valor_input > (valorAtual*0.25)){
    //Aqui você poem o aviso ou um alert
    alert('Maior que 25%');
}
    
09.09.2015 / 23:39
3

Do you need to test if the value is greater than 25% of the current or has more than 25% increase? In the second case, the 25% higher value can be obtained by multiplying the current value by 1.25, that is, just test if the value is greater than the current 1.25 times (or, if you want to leave it configurable: 1+ (plus / 100)) .

Something like this ...

if( valor_input > (valorAtual * 1.25)) {
   alert("Você informou um valor com mais de 25% de acréscimo");
}
    
09.09.2015 / 23:44
2

If you want to know if the value entered has increased more than 25% from the current value:

var result = valor_input > valorAtual*1.25;

If you want to know if the value entered is greater than 25% of the current value:

var result = valor_input > valorAtual*0.25;
    
09.09.2015 / 23:54
2

25% more than the current value would be:

valorAtual + valorAtual * 25 / 100

Then we have:

var valorInput = 25;
var valorAtual = 50;
var valorAMais = valorAtual + valorAtual * 25 / 100;

if (valorInput > valorAMais) {
    alert("È 25% maior");
}

The @PerryWerneck formula is also valid.

    
10.09.2015 / 01:01