javascript - Why does 6.00 + (plus) 5.00 = "5.006.00"? - Stack Overflow

admin2025-04-21  0

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.

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.

Share Improve this question edited Aug 20, 2019 at 20:54 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Mar 16, 2012 at 19:08 Daniel HunterDaniel Hunter 2,9567 gold badges30 silver badges34 bronze badges 8
  • 1 What happens if you set all_total = parseFloat(names_each) + parseFloat(all_total) it looks like all_total may be a string which is why it is doing a string concat – jzworkman Commented Mar 16, 2012 at 19:11
  • If you want to perform addition, both operands have to be numbers. Otherwise string concatenation is performed. And unless $ind_name_each contains a string with quotes, such as '"5"', names_each will already be a number. – Felix Kling Commented Mar 16, 2012 at 19:12
  • BTW: only all_total = '6.00' and names_each = '5.00' will give you these results – Aprillion Commented Mar 16, 2012 at 19:15
  • this question is related to PHP and not Javascript... please re-tag it... – Fahim Parkar Commented Mar 16, 2012 at 19:19
  • 1 is that a number or string?: Strings literas are denoted by quotation marks " or '. You really should have a look at the basics of JavaScript in the MDN JavaScript Guide. – Felix Kling Commented Mar 16, 2012 at 19:21
 |  Show 3 more ments

6 Answers 6

Reset to default 5

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

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

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

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.

On your examples it's not clear where all_total es 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.

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

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 parison with no auto-type casting.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745172207a288726.html

最新回复(0)