﻿ Rules for arithmetic operators in Caché | InterSystems Developer Community
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.

0
0 381
Discussion (7)1

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"

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

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

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

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?

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.