# "var" doesn't work as number

## Recommended Posts

Hi! My problem is that I've made a function with "var kgTotales = 0". But when I sum "kgTotales = kgTotales + 2" the result is "02", it returns a string and not a number. I use this var in a bucle so the result is "kgTotales = 02...." and next to it the next numbers I want to sum.

This is the code:

And this is the result:

I don't understand why it happens because precioTotal is a similar var and also shows a sum and works well. I hope someone could helps me.

Regards!

Edited by sevillano665

##### Share on other sites

Are you checking for errors in your console? What debugging are you doing? You should trace the value of precioTotal in your script and make sure you are getting the right values you expect.

console.log('precioTotal before => ' + precioTotal);console.log('n1 is => ' + n1 + ', n2 is => ' + n2);precioTotal = precioTotal + (n1*n2);console.log('precioTotal after => ' +  precioTotal);

##### Share on other sites

Any time you get a value from a form element or anything else on the page, the value is always a string. You can use functions like parseInt, parseFloat, or Number to convert to a number.

##### Share on other sites

Are you checking for errors in your console? What debugging are you doing? You should trace the value of precioTotal in your script and make sure you are getting the right values you expect.

console.log('precioTotal before => ' + precioTotal);console.log('n1 is => ' + n1 + ', n2 is => ' + n2);precioTotal = precioTotal + (n1*n2);console.log('precioTotal after => ' +  precioTotal);

The table multiply the 2nd column and the third one and the result is shown in the fourth, when I execute and write the values, the result is correct, so it's not possible there's some error en n1 or n2 I think, and precioTotal shows the correct result too. I think I'm doing something wrong and it doesn't recognize the var as a string.

Any time you get a value from a form element or anything else on the page, the value is always a string. You can use functions like parseInt, parseFloat, or Number to convert to a number.

Like this?

parseFloat(kgTotales);kgTotales = (kgTotales+n2);
Edited by sevillano665

##### Share on other sites

if you expect them to be whole numbers, I would use parseInt, with a base 10 radix

parseInt("2", 10);

##### Share on other sites

kgTotales is already a number. The strings you need to convert to numbers are n1 and n2.

##### Share on other sites

if you expect them to be whole numbers, I would use parseInt, with a base 10 radix

parseInt("2", 10);

It doesn't work, the same result

kgTotales is already a number. The strings you need to convert to numbers are n1 and n2.

I realized it and I changed, but the result is the same too :S

##### Share on other sites

Then show your updated code. If it isn't working then you didn't do it right. There are examples of using things like parseInt online if you want to research that.

##### Share on other sites

Then show your updated code. If it isn't working then you didn't do it right. There are examples of using things like parseInt online if you want to research that.

Here is:

And this is the view, the column where are 3 and 6 are "kg" and its sum have to be shown in kgTotales box, and the last one is for the price.

Regards!

##### Share on other sites

you aren't even using parseInt in that code. Show us the code you are actually using with the most recent attempt you've made. We can't help you move forward if you're showing us stale code.

##### Share on other sites

There's parseFloat instead. Both parseInt and parseFloat will return NaN (not a number) if the value could not be converted to a number. So, it sounds like the values in those fields are not numeric values. You can use the isNaN function to check if a value is NaN. If it is NaN, then show an error message that they have to enter a numeric value.

##### Share on other sites

oh woops, yes you're right. parseFloat is there.

##### Share on other sites

There's parseFloat instead. Both parseInt and parseFloat will return NaN (not a number) if the value could not be converted to a number. So, it sounds like the values in those fields are not numeric values. You can use the isNaN function to check if a value is NaN. If it is NaN, then show an error message that they have to enter a numeric value.

But I really want that the result text boxes of every multiplication contain "0" if I don't introduce any number, in this way the sum will be made. I have tried to put by default a "0" in the result text boxes but when I introduce some number in some text box all result text boxes shows "NaN" and the sum is not made.

Edited by sevillano665

##### Share on other sites

Then check if the result is NaN and, if so, replace it with 0.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.