Jump to content

Double Numbers Not Working Right


glenelkins

Recommended Posts

i get 10.5

<script type="text/javascript">
var price = 0.50;
var delivery = 10.00;
price = price + delivery;
alert(price);
</script>

 

how are you getting these numbers? from text fields or something? cus then they would be strings and javascript will use concatenation. make sure they are float numbers with parseFloat():

<script type="text/javascript">
var price = document.getElementById('price').value;
var delivery = document.getElementById('delivery').value;
price = parseFloat(price) + parseFloat(delivery);
alert(price);
</script>

hehe i just posted something similar, what is happening is that your variables are strings, so it's adding two strings together, not numbers.

 

a way that i was to to fix it was

 

price = +price + +delivery;

it turns them from a string into a integer.

i hope this helped.

hehe i just posted something similar, what is happening is that your variables are strings, so it's adding two strings together, not numbers.

 

a way that i was to to fix it was

 

price = +price + +delivery;

it turns them from a string into a integer.

i hope this helped.

 

interesting...i've never seen a double plus used like that before. i always use parseInt(). but also, the OP needs FLOATs, not INTs

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.