Question Ponnumani Gurusamy · Aug 17, 2016

Rules for arithmetic operators in Caché

write "5apples" + "7 orange" //Ans :12

Values are string , why  add numeric value . its an error or correct . please tell me.

Comments

Dmitry Maslennikov · Aug 17, 2016

because + is an operation for numbers, and yes, answer is correct, because 5 + 7 is 12

if you expected to see "5apples7 orange", in this case you should use concatenate operator "_"
write "5apples" _ "7 orange"

0
Ponnumani Gurusamy  Aug 17, 2016 to Dmitry Maslennikov

Thank you for your guidance . But "" its mentioned string . how to add a string ?

0
Dmitry Maslennikov  Aug 17, 2016 to Ponnumani Gurusamy

if you need add a string, you should concatenate it with operator _

+ converts all types to number, and doing arithmetical operator add

7_"" became "7"
+"5apples" became 5

0
Evgeny Shvarov · Aug 17, 2016

Hi, Ponnumani. I've changed the title  to closer to the question version.

0
Sebastian Mueller · Aug 24, 2016

AFAIK everything is a string, it's just parsed prior to being processed. So it sees the 5, then characters it omits, same for the other string, so you are left with two numbers (which probably are processed as floats) which the + operator adds. Is that correct?

0
Carlos Lopes · Aug 24, 2016

Numeric operators convert their arguments to numbers (according to well defined rules) before performing the operation. That's why "5apples"+ "7 orange" gives 12.
Conversely, string operators convert their arguments to strings before the operations is performed.

In other words, regardless of the arguments, the operator automatically performs the necessary conversions according to what it expects.

0