How to subtract decimal numbers?

Asked

Viewed 1,416 times

3

I did a function to fade out on <div> once I finish loading the page but can’t get the exact value of the end of Fadeout (that would be 0) always gives me a number like this: 0.009999999999247;

How can I subtract correctly and get 0?

function fadeOut(id){
    //pega o elemento da página
    obj = document.getElementById(id);
    //seta a opacidade como 0
    obj.style.opacity = 1.00;

    fazFadeOut = function (){
        if(obj.style.opacity != 0){  
            var atual = Number(obj.style.opacity);   
            var newopac = atual - Number(0.01);
            obj.style.opacity = String(newopac);    
            if(obj.style.opacity == 0){
                obj.style.display = 'none';
            }
            setTimeout('fazFadeOut()', 10);
        }
    }
    fazFadeOut();
}

EXAMPLE IN OPERATION

  • the expression atual - Number(0.01) that returns you 0.00999... ?

  • @Paulo Sim, was to set the opacity of <div> 0 but give me this 0.009999999999999247

  • This is actually a stopgap. Money should not be represented in this way: https://answall.com/q/110463/101.

2 answers

5


Javascript number is a floating point (http://www.hunlock.com/blogs/The_Complete_Javascript_Number_Reference) and, according to the link "as accurate as possible". Therefore:

var soma = 0.06 + 0.01
// soma fica com o valor de 0.06999999999999999

One solution to this is to round up the value using toFixed(numeroDigitsNaCasaDecimal)

var soma = (0.06 + 0.01).toFixed(2);
// soma fica com o valor de 0.07

var maisCasas = (0.06 + 0.01).toFixed(5);
// maisCasas fica com o valor de 0.07000
  • 1

    Thanks @Ricardo, it solved :) I only had to change String(newopac) to Number(newopac). toFixed(2);

2

You can use Math.floor() to round down to obtain the 0 that you want:

But you want the decimal value, so you should only use fewer decimal places, because javascript is no longer needed after a few decimal places, so:

window.onload = function(){
//quando o documento terminar de carregar esconde a div
    setTimeout(function(){
        fadeOut('load');
        document.getElementById('content').style.webkitFilter = 'none';
    },1000);
};

function fadeOut(id){
    //pega o elemento da página
    obj = document.getElementById(id);
    //seta a opacidade como 0
    obj.style.opacity = 1.00;

    fazFadeOut = function (){
        if(obj.style.opacity !== 0){     
            var atual = Number(obj.style.opacity);   
            var newopac = atual - Number(0.01).toFixed(x);//onde x seria o numero de casas decimais que você quer.
            obj.style.opacity = String(newopac);    
            if(obj.style.opacity === 0){
                obj.style.display = 'none';
            }
            setTimeout(fazFadeOut, 10);
        }
    };
    fazFadeOut();
}

Observing:

I fixed some warnings that your Jsbin reported, the code would be as above already fixed.

Changes: comparisons should be made with === and !== in this context, and also missing some semicolons. Something else important don’t use setTimeout("fazFadeOut()", 10); never. You must pass the function itself, which would be using setTimeout(fazFadeOut, 10);

  • ok - I had not understood right, sorry. I edited my reply :)

  • Thank you so much for helping @Paulo!

Browser other questions tagged

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