Why does 6.00 + (plus) 5.00 = “5.006.00”?

▼魔方 西西 提交于 2020-01-16 00:54:15

问题


If I have a variable that is given a "numeric" value from a PHP echo...

var names_each = <?php echo $ind_name_each; ?>;

...and $ind_name_each is derived from a MySQL column of type decimal(5,2), is it a number or a string in JavaScript?

if all_total = 6.00 and names_each = 5.00 and I do this:

all_total = parseInt(names_each) + all_total;

I get 56.00

all_total = names_each + all_total;

I get 5.006.00

all_total = parseFloat(names_each) + all_total;

I get 56.00

I need some understanding here.


回答1:


Both variables are strings:

var names_each = '5.0', all_total = '6.0';

so the + operation concatenates those strings:

console.log(names_each + all_total);  // '5.0' + '6.0' => '5.06.0'
console.log(parseInt(names_each) + all_total); // 5 + '6.0' => '5' + '6.0' => '56.0'

but if you parse them to numbers first, then you can use + to add them:

all_total = parseInt(names_each) + parseInt(all_total);
console.log(all_total);  // 5 + 6 => 11



回答2:


Convert all_total from string to int / float too...

Because now, + in all three examples is string concatenation.




回答3:


In JavaScript, if either side of the + operator is a string value then both sides are converted to a string and the result of the operator is the string concatenation of those values. Here are some examples:

 1  +  2   // 3
"1" + "2"  // "12"
"1" +  2   // "12"
 1  + "2"  // "12"

Note that the last 3 cases have the same result.

Happy coding.




回答4:


On your examples it's not clear where all_total comes from in the first place, but it must be a string, since you are getting string concatenation instead of addition.

To answer your first question, names_each is not a string, it's a number.

The output of this PHP file

var names_each = <?php echo $ind_name_each; ?>;

Should be something like this:

var names_each = 5;

or

var names_each = 5.1;

So, it's not a string, but an actual number in js. If the other side of your attempted addition is a string, you get string concatenation.




回答5:


Your variables are strings, not numbers.
Therefore, unless you explicitly convert them to numbers, you get string concatenation.




回答6:


The following will tell you what they are:

console.log(typeof names_each);
console.log(typeof all_total);

Here are some examples:

typeof "6.00" // The result is "string"
typeof 6.00 // The result is "number"

If you add some logging to your application, you should be able to see where it is turning into a string.

Also, you should know that the following occurs:

5.00 == "5.00" // The result is "true"
5.00 === "5.00" // The result is "false"

Using the triple equals prevents the JavaScript engine from implicitly casting the type of your variable. So, with === you will get a strict comparison with no auto-type casting.



来源:https://stackoverflow.com/questions/9743145/why-does-6-00-plus-5-00-5-006-00

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!