Sum of values in variable does not occur, tofixed

Asked

Viewed 559 times

2

It was supposed to be adding up.. but it didn’t, I added the parsefloat Why was giving error that it was not a function..

var x = 0;
x = parseFloat((x + 0.4)).toFixed(1); // 0.4
x = parseFloat((x + 0.4)).toFixed(1); // 0.8
alert(x); // 0.8
x = parseFloat((x + 0.4)).toFixed(1); // 1.2
alert(x); // 1.2

should be like in the comments but.. what’s the problem? Tofixed is doing something wrong? It always returns 0.4

http://jsfiddle.net/rtmobcz5/

2 answers

4


The problem is that you are concatenating strings, (return from toFixed) and not adding up numbers:

var x = 0;
x = parseFloat((x + 0.4)).toFixed(1); // "0.4"    <-saída verdadeira
x = parseFloat((x + 0.4)); // "0.40.4" que depois de parseFloat e toFixed vira "0.4" novamente

The first "sum" gives "0.40.4", and the parsefloat takes as far as it understands (the first "0.40.").

One solution would be this:

var x = 0;
x = parseFloat((x + 0.4).toFixed(1) ); // 0.4
document.body.innerHTML += x + '<br>';
x = parseFloat((x + 0.4).toFixed(1) ); // 0.8
document.body.innerHTML += x + '<br>';
x = parseFloat((x + 0.4).toFixed(1) ); // 1.2
document.body.innerHTML += x + '<br>';

Notes:

  • The "tofixed" in this case could be applied in another order, for example when showing on screen only. I only kept it in the code so you could compare it to the original.

  • I switched the alert for innerHTML just to make it easier to read here on the page.


Making it even simpler:

var x = 0;
x += 0.4 // 0.4
document.body.innerHTML += x.toFixed(1) + '<br>';
x += 0.4 // 0.8
document.body.innerHTML += x.toFixed(1) + '<br>';
x += 0.4 // 1.2
document.body.innerHTML += x.toFixed(1) + '<br>';

Here we simply take the parseFloat, because the toFixed was used only when displaying on screen, without disturbing the sums.

  • what function parsefloat in all this then, just turn string?

  • 1

    no, the parsefloat is just the reverse. the problem was the . tofixed off the ( ). ParseFloat, the way I did, makes toFixed again a number. The way you did, he turned number, but then the. toFixed made turning string again; i.e., parsefloat has to be applied after tofixed to be able to sum, and not before.

  • 1

    @user3163662 complemented the answer with one more example, see if help.

1

The problem is that these numbers are in format string and not number.

What these methods return:

Accepts a string and convert to number without rounding. The similar method that rounds to integers is the parseInt(string, base);. Returns in format number.

Accepts a number with decimals (fractional part). Returns a string.

Suggestion:

I suggest creating a function that can do this, and that can account for different cases:

function soma(a, b, casasDecimais) {
    if (typeof a == 'string') a = parseFloat(a);
    if (typeof b == 'string') b = parseFloat(b);
    var soma = a + b;
    return soma.toFixed(casasDecimais);
}

jsFiddle: http://jsfiddle.net/07j4t8ag/

Browser other questions tagged

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