Pregunta

I have some variables, which all contain numbers, and they are all parseFloat's.

In the end, all the parseFloat's are calculated into one variable, and from that variable, I need some function, that settles the specific number, into every fifty(50).. So if the specific number is 163, it needs to be settled to 150. and if its 243, it needs to be settled to 200..

This is actually working, but there is a problem tho.. The browser freezes and tells the script are LOOPing ?? I'm thinking it's because the variable betalTotal, is containing a comma number ?? like 6.5 ?

This is what I'm having so far:

iAlt = 0;

iAlt = maxregning - pakkerTotal - betalTotal;

//parseFloat(iAlt);
/* - THIS AINT WORKING 
if(betalTotal == 6.5){
    betalTotal.val(betalTotal.val().replace('6.5', '6,5'))
}*/

while(iAlt % 50 != 0){
    iAlt--;
}
$('#sk').text(iAlt);

Does anyone know a solution for this?

¿Fue útil?

Solución

Divide the value by 50, round it, and multiply by 50:

iAlt = Math.floor(iAlt / 50) * 50;

(Your original method would also work if you rounded the value before the loop, but the loop is not needed at all this way.)

This will always round down, i.e. 243 to 200. If you want to round to the closest, i.e. 243 to 250, you would use Math.round instead.

Otros consejos

iAlt % 50 will never work for you with Float.

parseFloat("30.3")%2 =     0.3000000000000007

you can use Round , ceil , floor

You need to compute with Math.floor instead of Math.round, here is an test page.

iAlt = Math.floor(iAlt / 50) * 50
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top