2

Possible Duplicate:
Addition turns into concatenation

Here's what I have...

    var srate = Math.round(princ * intr * term * 100) / 100; //works fine
    var dasvalue = princ + srate; //doesn't work
    document.calc.pay.value = dasvalue;

The "var dasvalue = princ + srate;" adds the two sums up as strings.

100 + 1.4 = 1001.4

What am I doing wrong?

Community
  • 1
  • 1
Omar Little
  • 71
  • 2
  • 5
  • possible duplicate of [Addition turns into concatenation](http://stackoverflow.com/questions/6609142/addition-turns-into-concatenation) and [Addition is not working in JavaScript](http://stackoverflow.com/questions/8377410/addition-is-not-working-in-javascript) and [Preventing concatenation](http://stackoverflow.com/questions/7784784/preventing-concatenation). – Felix Kling Aug 09 '12 at 13:02

3 Answers3

4

You can use the unary plus operator to cast to type Number, ensuring addition rather than concatenation:

var dasvalue = +princ + +srate;
chrisfrancis27
  • 4,516
  • 1
  • 24
  • 32
  • Although this is perfectly right, this is wrong on so many levels. I'd probably hit the developer writing such code. This is not readable and will probably break with compression.`+ +srate` could turn into `++srate`. – Torsten Walter Aug 09 '12 at 12:59
  • Don't use such compressor, use google closure, yahoo compressor or similar that actually understands javascript. – Esailija Aug 09 '12 at 13:00
  • @TorstenWalter if the compressor can't handle adding a value preceded with a unary plus, it's not really much of a compressor is it... – chrisfrancis27 Aug 09 '12 at 13:01
  • Although, you are right that the second value `srate` doesn't need the unary operator, as addition uses left-to-right operator precedence. – chrisfrancis27 Aug 09 '12 at 13:04
  • 1
    @ChrisFrancis all the `+` are necessary here if both of those are strings – Esailija Aug 09 '12 at 13:05
  • HOLY SH*** you're right! :) My bad. Ignore that last comment. – chrisfrancis27 Aug 09 '12 at 13:06
  • We use both, YUI and closure, for different projects ;). This still doesn't fix the readability though. As I said, this is perfectly valid and right, so is binary math, which doesn't mean you should use it if you care about maintainable code. Code like this is so easy to mess up and to spend hours on debugging because it's so hard to see what's going on. `/rant over` This probably isn't the place having such a conversation though. – Torsten Walter Aug 09 '12 at 13:35
  • You're absolutely right that maintainability of code shouldn't be an afterthought. Your example is more readable, so I've upvoted it. – chrisfrancis27 Aug 09 '12 at 13:37
  • 1
    @ChrisFrancis Thank you Sir :) +1 for your answer since it's indeed correct. To satisfy both world views, one could argue that maybe the unary operator should be used when assigning the variables `princ = + field.value; dasvalue = princ + srate;` gives you a better sense of what's going on. – Torsten Walter Aug 09 '12 at 15:03
2

princ is a string too. You can convert it to a Number with the unary + operator.

alex
  • 479,566
  • 201
  • 878
  • 984
2

If your value in princ comes from an input you need to convert it into a number first.

var dasvalue = Number(princ) + srate;
Torsten Walter
  • 5,614
  • 23
  • 26