Incorrect adding two numbers in JavaScript

Global.alert("base: " + base + ", upfront: " + upfront + ", both: " + (base + upfront)); 

The above code outputs something like:

 base: 15000, upfront: 36, both: 1500036 

Why does it combine two numbers instead of adding them?

Ultimately, I want to set the value of another field to this amount using the following command:

 mainPanel.feesPanel.initialLoanAmount.setValue(Ext.util.Format.number((base + upfront), '$0,000.00')); 

And when I try to do this, it turns the number into millions instead of 15,036.00. Why?

+14
javascript
Sep 03 '10 at 17:23
source share
6 answers

This can happen because these are strings. Try to parse them:

 Global.alert( "base: " + base + ", upfront: " + upfront + ", both: " + (parseInt(base) + parseInt(upfront)) ); 

If these numbers are decimal, you will need parseFloat .

+12
Sep 03 '10 at 17:25
source share

A simple example:

  1 +1 == 2 "1"+1 == "11" "1"*1 + 1 == 2 

Ways to turn a string into a number:

  • parseInt(str)
  • parseInt(str,10)
  • parseFloat(str)
  • +str
  • str*1
  • str-0
  • str<<0
  • Number(str)

And here are some of the consequences: Results of converting various strings using the above techniques
(source: phrogz.net )

Number(str) has the same behavior as str*1 , but requires a function call.

I personally use *1 , since it is short for input, but still stands out (unlike unary +), and either gives me what the user typed in or completely fails. I use parseInt() only when I know that there will be non-numeric content at the end that I need to ignore, or when I need to parse a non-base-10 string.

You can check their performance in your browser on my example page .

+25
Jan 05 2018-11-11T00:
source share

Try

 Global.alert( "base: " + base + ", upfront: " + upfront + ", both: " + (parseInt(base,10) + parseInt(upfront,10)) ); 

10 indicates base 10, otherwise it is likely that a value processed as octal exists.

+6
Sep 03 '10 at 17:26
source share

It treats it as a string. You must do your math before the line. Example:

 base + upfront + ' string' 

will return the string "15036".

 string + base + upfront 

will return the string 1500036, as you see now.

Or use parseInt () .

+1
03 Sep '10 at 17:26
source share

http://jsperf.com/number-vs-parseint-vs-plus/3

It may also be of interest to you. This is just a comparison of the performance of the methods already mentioned here.

+1
03 Sep '10 at 17:30
source share

I do not know why brackets do not help you.
If i try

 var base = 500; var upfront = 100; alert("base: " + base + ", upfront: " + upfront + ", both: " + (base + upfront)) 

As an answer, I get 600, so maybe something is happening in the Global.alert function?

One of the design mistakes of the language is that + is an addition operator and a concatenation operator. Combined with the fact that he is freely typed and will act implicitly, this means that he can give you some unpleasant surprises if you do not take steps to make sure that you are really adding numbers, not concatenating strings. In this case, it treats your + upfront base as strings and therefore concatenates.

In any case, the path around it may be to have (base - upfront*-1) .

+1
Sep 03 '10 at 17:39
source share



All Articles