0

Pls consider the calculation in javascript code below. With example values:

  • investmentfix = 200.00
  • investmentvar = 123.12
  • nrofparts = 5.00

...the console shows a strange result value 0200.00615.6. which I don't understand. I noticed the values 200.00 and 615.6, - the latter the result of investmentvar * nrofparts- in this result value. But I'd expect (and target to get) the result 815.6 (200+(123.12*5.00).

What goes wrong? Does this relate to a kind of format issue?

javascript code:

var result =0;

result += (investmentfix + (investmentvar * nrofparts));

console.log(result);
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
Joppo
  • 715
  • 2
  • 12
  • 31
  • 4
    Are some of your variables strings? It may be trying to concat rather than add them. –  Aug 07 '13 at 15:25
  • can we see some more code? – imulsion Aug 07 '13 at 15:25
  • ^^ They would be strings if they come directly from user inputs. –  Aug 07 '13 at 15:26
  • `investmentfix` must be a string here: `0 + "200.00" + 615.6` would yield `0200.00615.6`. – apsillers Aug 07 '13 at 15:26
  • Try the following: `result = parseFloat(0,10); result+=parseFloat(investmendfix,10)+parseFloat(investmentvar*nrofparts,10);` – Jeff Noel Aug 07 '13 at 15:26
  • full code would help but definitely some decimal formatting issue – topcat3 Aug 07 '13 at 15:26
  • yes. I assume I need to use somethings like parseFloat() to convert to float (?) (just googled that function) – Joppo Aug 07 '13 at 15:28
  • @user2543182 If you just use the `+` sign before a variable it'll convert it to number, so `+"200.3"` will return 200. `parseFloat` will, for instance parse incorrect input like `200asdgwsed` as `200` where putting a `+` in front of it (or calling `Number("200abs")` ) will return `NaN` indicating a problem. – Benjamin Gruenbaum Aug 07 '13 at 15:30
  • Possible duplicates: [how to sum two numbers from input tag?](http://stackoverflow.com/questions/11961474/how-to-sum-two-numbers-from-input-tag/), [How to add two strings as if they were numbers?](http://stackoverflow.com/questions/8976627/how-to-add-two-strings-as-if-they-were-numbers) – apsillers Aug 07 '13 at 15:31

1 Answers1

1

Try the following:

result = parseFloat(0,10);
result+=parseFloat(investmendfix,10)+parseFloat(investmentvar*nrofparts,10);
console.log(result);

This ensures that the JavaScript engine parses the variables at numbers instead of strings.

parseFloat() syntax: parseFloat(myNumber,base);

Jeff Noel
  • 7,500
  • 4
  • 40
  • 66
  • 1
    execllent, it works; thnx for all your comments (!) I just first used only parseFloat on the individual parameters when getting the values from the form fields. But it seems -as indicated by the solution of Jeff- that one still needs to apply parseFloat on the calculation as well. – Joppo Aug 07 '13 at 15:43
  • Everything would work as intended without the need of `parseFloat()` if the variables would not contain strings. @user2543182 You can find which variable is a string by using `typeof(myVariable) == number`. If it returns true, the variable is a number, otherwise it holds another type of value: `string`, `Object`, `Array`, etc. Finding which variable isn't a string could prevent the need of using `parseFloat()` thrice. – Jeff Noel Aug 07 '13 at 16:45