Previous Thread
Next Thread
Print Thread
Displaying the Total of 2 Numeric Values #36928 07 Jan 24 11:51 PM
Joined: Mar 2005
Posts: 494
Ty Griffin Offline OP
Member
OP Offline
Member
Joined: Mar 2005
Posts: 494
A forum-shy developer says:

Found an interesting display problem the other day. Don't know if it has always been there or has shown up with recent updates. It has to do with displaying the total of 2 numeric values. We have always been able to create a string using 'string = num using mask'. However if you try doing it using 'string = (num1+num2) using mask' what you get in the string is the correct digits but the number has been multiplied by 10. In order to get the correct string value you now need to use 'string = STR(num1+num2) using mask'. I have attached a test program that you can use to check it out. This occurs whether the display is whole numbers or has decimal points in it. I first thought it had to do with the mask that was being used but finally found that you need to use the STR() format now. Is the STR required or is there a problem with the conversion using the (num1+num2) format? If STR() format is required I think the compiler should be giving you some sort of error message.

Re: Displaying the Total of 2 Numeric Values [Re: Ty Griffin] #36929 08 Jan 24 01:43 AM
Joined: Jun 2001
Posts: 11,794
J
Jack McGregor Offline
Member
Offline
Member
J
Joined: Jun 2001
Posts: 11,794
The issue here is a consequence of the automatic conversion of strings to numbers and vice versa according to the context, combined with the fact that the + operator acts as addition or concatenation, again depending on the context. There are at least two scenarios where this can cause you problems.

One is that most operators and functions expect a certain type of argument, and if that doesn't match the type supplied, it inserts a STR() or VAL() operation to convert the argument. So for example, LEN(987) results in 3 because it gets converted by the compiler into LEN(STR(987)), i.e. LEN("987"). Usually that's intuitive. The one place where it isn't is with the STR() function. Since it expects a numeric argument, if you pass it a string, it inserts a VAL() operation. So for example, STR("ABC") becomes STR(VAL("ABC")) which is zero, not "ABC". See this thread for another discussion of the problem in which we entertained the possibility of changing the behavior to be more intuitive (even if less consistent), but I think ended up deciding not to out of fear of breaking backward compatibility.

Your solution of STR(num1 + num2) works because of a variation of the same non-intuitive concept, i.e. it expects numeric arguments to it sets the expression mode to numeric, thus overriding the string mode set initially by the string result variable.

The other one is accidental concatenation. The expression string = (num1+num2) starts out with a string variable, which sets the expression mode to string. That in turn causes the subsequent operands num1 and num2 to be converted to string, which then causes the + to work as concatenation. To avoid that, you either have to change the result of the expression to be a numeric variable, or insert explicit conversion functions, or use the Explicit Plus Operators, or the NUMEXPR$() function.


Moderated by  Jack McGregor, Ty Griffin 

Powered by UBB.threads™ PHP Forum Software 7.7.3