Android Question Adding 2 string decimals gives integer result

sconlon

Active Member
Licensed User
Longtime User
What is the 'correct' way to add two string values together? For various reasons that I won't go into I have to add 2 string values together and assign to a third string, something like:

B4X:
str1 = "6.50"
str2 = "0"
str3 = str1 + str2

which results in str3 = "6".

Is it necessary to assign the string to doubles and then use numberformat2 to convert the result back to a string, eg

B4X:
dbl1 = str1 : dbl2 = str2
dbl3 = dbl1 + dbl2
str3 =  NumberFormat2(dbl3,0,2,2,True)
 

klaus

Expert
Licensed User
Longtime User
What result do you expect ?
Do you want to do an arithmethic operation or concatenate two strings ?
If you want to do arithmethic operations you should use Doubles.
If you want to concatenate two strings you should use dbl3 = dbl1 & dbl2.
 
Upvote 0

sconlon

Active Member
Licensed User
Longtime User
Thanks Klaus. It was an arithmetic operation I wanted so doubles is the way. But just to satisfy my curiosity, how does the string example above give a result of "6"?
 
Upvote 0
Top