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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

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

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

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

×
×
  • 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.