Concatenation with Addition in It Doesn't Work as Expected

Concatenation with addition in it doesn't work as expected

+ and . have the same operator precedence, but are left associative. Means after the first concatenation:

'(' . (int)$data['event_id']

The string got added with your key, e.g.

"($data['event_id']" + $key

So the string gets converted into an integer in that numerical context and disappears. To solve this use parentheses () around your addition.

Concatenating strings doesn't work as expected

Your code, as written, works. You’re probably trying to achieve something unrelated, but similar:

std::string c = "hello" + "world";

This doesn’t work because for C++ this seems like you’re trying to add two char pointers. Instead, you need to convert at least one of the char* literals to a std::string. Either you can do what you’ve already posted in the question (as I said, this code will work) or you do the following:

std::string c = std::string("hello") + "world";

concatenation in sql not working as expected

Since you are using MySQL, you've got to use the CONCAT function to concatenate your strings, not the + operator.

select a.name, a.phone, b.mobile, b.relation,
case
when a.phone<>'' and b.mobile<>'' then CONCAT(a.phone, ' ', b.mobile)
when a.phone<>'' and b.mobile='' then a.phone
when a.phone='' and b.mobile<>'' then b.mobile
else ''
end as phone
from abc a join bcdb where a.id=b.id and b.relation='a123'

Remark: You should take care that none of the operands should be NULL.

Javascript (+) sign concatenates instead of giving sum of variables

Use this instead:

var divID = "question-" + (i+1)

It's a fairly common problem and doesn't just happen in JavaScript. The idea is that + can represent both concatenation and addition.

Since the + operator will be handled left-to-right the decisions in your code look like this:

  • "question-" + i: since "question-" is a string, we'll do concatenation, resulting in "question-1"
  • "question-1" + 1: since "queston-1" is a string, we'll do concatenation, resulting in "question-11".

With "question-" + (i+1) it's different:

  • since the (i+1) is in parenthesis, its value must be calculated before the first + can be applied:

    • i is numeric, 1 is numeric, so we'll do addition, resulting in 2
  • "question-" + 2: since "question-" is a string, we'll do concatenation, resulting in "question-2".

Vue computed properties, issues with addition vs concatenation

Just use Number object constructor in order to make the sum :

  computed: {
c: function() {
return Number(this.a) + Number(this.b);
}
}

the two operands are considered as strings which will be concatenated when you try to put + between them, in order to avoid that default behavior try to use Number constructor or parseInt, parseFloat function to change the behavior to sum operation.

or try to use number modifier in v-model directive like :

<input type="number" v-model.number="b" style="color: white" />

How to ensure javascript addition instead of string concatenation (Not always adding integers)

Try using parseFloat. The input variables will be converted to floats, which will work whether the string contains an integer or a float. Your result will always be a float.

Javascript: + sign concatenates instead of giving sum of variables

you are dealing with strings here, and math operations on strings will mess up. Remember when ever you are doing math operations you have to convert the data into actual numbers and then perform the math.

Use parseInt() more Details here

Your code should change to

Vfinal = parseInt(Vinitial,10) + parseInt(acceleration,10) * parseInt(time,10);

Edit 1: If the numbers are decimal values then use parseFloat() instead

So the code would be

Vfinal = parseFloat(Vinitial) + parseFloat(acceleration) * parseFloat(time);



Related Topics



Leave a reply



Submit