Percentage Com javascript

Asked

Viewed 4,026 times

0

Good afternoon, I have a stupid question and I need your help. I have a situation where I need to show if the value is 25% more than the current value. However, I’m having a hard time coming up with this logic. I did some research but I couldn’t find something that clarifies me:

var valor_input = 25;
var valorAtual = 50;
var result = DUVIDA;
alert(result);

4 answers

4


You can check by dividing by 4 or 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%');
}

3

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

Something like...

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

2

If you want to know if the inserted value has an increase greater than 25% in relation to the current value:

var result = valor_input > valorAtual*1.25;

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

var result = valor_input > valorAtual*0.25;

2

25 % more than the present 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.