-1

Possible Duplicate:
How do I Convert a String into an Integer in JavaScript?

i have the following code:

    $.ajax({
      type: "GET",
      url: 'index.php?act=ajax&op=getShippingPrice&id='+shippingID,
      success: function(data) {
        var currentPrice = Number($("#finalPrice").html());
        var newPrice = Number(currentPrice + data);
        $("#finalPrice").html(newPrice);
      }
    });

i try to calculate the new price. but i actually get a string of newPrice, which contains the current price, and right after it the data from ajax.

if the current price is 1500, and data from ajax is 10, what i get is 150010, and not 1510.

i also tried to use parseInt, probably didn't use it right.

Community
  • 1
  • 1
user1936192
  • 335
  • 2
  • 4
  • 10
  • Wrap each string in parseInt – mplungjan Jan 03 '13 at 08:40
  • Yes, it's most likely `$('#finalPrice').html()` which outputs a string, instead of an int, using `parseInt` will cast the string to an integer, and instead of the `+` operator `concatenating` a string, it will `add` to the integer. – Ohgodwhy Jan 03 '13 at 08:42
  • Both operands need to be numbers if you want to perform addition. – Felix Kling Jan 03 '13 at 08:43

5 Answers5

4

Use this:

Assuming a price with decimals, using parseFloat, if not, use parseInt.

$.ajax({
  type: "GET",
  url: 'index.php?act=ajax&op=getShippingPrice&id='+shippingID,
  success: function(data) {
    var currentPrice = parseFloat($("#finalPrice").html());
    var newPrice = currentPrice + parseFloat(data));
    $("#finalPrice").html(newPrice.toFixed(2));
  }
});
ATOzTOA
  • 34,814
  • 22
  • 96
  • 117
  • And now a toFixed and we are there – mplungjan Jan 03 '13 at 08:45
  • @mplungjan IMHO you yourself are making far too many assumptions about the OP's requirements. He said he tried `parseInt` so it's reasonable to expect that the fields are actually ints, at which point use of `parseFloat` and `toFixed` are inappropriate. – Alnitak Jan 03 '13 at 09:12
  • 1
    Inappropriate is a hard word to use when the thing is a price. It could be added to this solution that "Assuming a price with decimals, use parseFloat, if not, use parseInt" and that would handle any assumptions. – mplungjan Jan 03 '13 at 09:19
2

Do it like:

var newPrice = parseInt(currentPrice) + parseInt(data);
Arvind Bhardwaj
  • 5,231
  • 5
  • 35
  • 49
0

This will help You:

 var newPrice = 1*currentPrice + 1*data;

Multiply by 1 don't change the result but it will change the variable type.

bumerang
  • 1,776
  • 21
  • 30
0

Convert your HTML-derived value into a number first (and ensure you supply the radix parameter to cope with leading zeroes).

You should also remove any white space that might exist:

var currentPrice = parseInt($.trim($("#finalPrice").html()), 10);
var newPrice = currentPrice + data;

If the output from the server is not in JSON format then you should also convert the data field too before the addition:

data = parseInt(data, 10);

Finally, if your numbers aren't actually integers, use parseFloat instead of parseInt

Alnitak
  • 334,560
  • 70
  • 407
  • 495
-1

Try modifying like this one:

$.ajax({ type: "GET", url: 'index.php?act=ajax&op=getShippingPrice&id='+shippingID, success: function(data) {

    var newPrice = Number(currentPrice + Number($("#finalPrice").html()));
    $("#finalPrice").html(newPrice);
  }
});

At least you will get error.

Sarang
  • 171
  • 8