Amiga.org
Amiga computer related discussion => Amiga Software Issues and Discussion => Topic started by: Jettah on January 26, 2005, 05:57:18 PM
-
Hi,
Is there a proper way that guarantees me that the outcome of aroutine is a NUMERIC value?
I've built a routine that accepts *ANY* character, but after processing in a routine, it should consist of numerical characters ONLY.
So far I did this:
Value = TRANSLATE(Value, '.', ',') /* All (decimal) comma's turn into decimal points */
Pos = VERIFY(Value, '0123456789.')/* Pos becomes the index of the first character NOT contained in '012345678.' */
DO WHILE Pos > 0
Value = DELSTR(Value, Pos, 1) /* Delete that character */
Pos = VERIFY(Value, '0123456789.') /* Look for another such character */
END
/* Value is now formed guaranteed by the characters '0123456789.' *ONLY* */
/* But there may be more than ONE decimal-point left...*/
Pos = LASTPOS('.', Value) /* Find the last occurrence of '.'in Value */
DO WHILE INDEX(Value, '.') < Pos /* Loop if there is more than ONE '.'in Value */
Value = DELSTR(Value, Pos, 1) /* Delete that redundant decimal-point */
Pos = LASTPOS('.', Value) /* Again, look for another instance of '.' */
END
/* Value consists now ONLY of the characters 0 - 9, and at most ONE decimal-point.
**It should now by all accounts yield a NUMERIC value (IMHO), but...
**...it sometimes presents me with an "Arithmetic Conversion Error"
*/
Can anyone, with ample knowledge of ARexx, give me clue?
Regards,
Jettah
-
First I'd need to know where this mystery number is coming from. Sight unseen, instead of trying to fix broken logic by converting some unknown type to a numeric type, just make sure whatever logic is generating it is generating what you want in the first place.
-
Are you sure there aren't other characters in the string (spaces, etc.?).
-
Why not just put a:
say value
Before the point you get the error to see exactly what is causing it?
-
Are you sure there aren't other characters in the string (spaces, etc.?).
Why not just put a: say value Before the point you get the error to see exactly what is causing it?
This part in the code ensures for the full 100% that the contents of Value consists of the characters 0 - 9 and '.''s:
Pos = VERIFY(Value, '0123456789.')/* Pos becomes the index of the first character NOT contained in '012345678.' */
DO WHILE Pos > 0
Value = DELSTR(Value, Pos, 1) /* Delete that character */
Pos = VERIFY(Value, '0123456789.') /* Look for another such character */
END
So there are *NO* other characters then the allowed ones.
But I found this odd behaviour:
In = 12a34 --> Out = 1234 --> DATATYPE = NUM
In = 22...45.7 -->Out = 22.457 --> DATATYPE = NUM
Now the odd one:
In = "34,56" --> Out = 34.56 --> DATATYPE = CHAR
In my opening post you can see that comma's are TRANSLATEd to points by this (the first) line:
Value = TRANSLATE(Value, '.', ',') /* All (decimal) comma's turn into decimal points */
The final part of the code presented:
Pos = LASTPOS('.', Value) /* Find the last occurrence of '.'in Value */
DO WHILE INDEX(Value, '.') < Pos /* Loop if there is more than ONE '.'in Value */
Value = DELSTR(Value, Pos, 1) /* Delete that redundant decimal-point */
Pos = LASTPOS('.', Value) /* Again, look for another instance of '.' */
END
...ensures that there is only *ONE* '.' allowed.
As I said before, IMHO, a string of '12.34' should yield NUMERIC.
I found the culprit to be in use of the comma. ANY string containing a comma will NEVER turn into a string that yields NUMERIC! Whatever processing you do to that string! Even copying it to another variable, when it LOOKS NUMERIC, will turn that string in the other variable into DATATYPE = CHAR!
Strange, but it is.
I also found a way, albeit a very, very elaborate one, to overcome this situation. I'll post it later on, as I have to make somefinal tests first.
May arise the question of why putting in a comma in a numerical field in the first place. This question arises mainly in the Anglo-Saxon part of the world. 10 million currency units minus 1 cent is written this way:
Anglo-Saxon style: 9,999,999.99
Rest of the world: 9.999.999,99
As I am living in a country that is part of the latter group, decimals are separated from integers by a comma. Hence a numerical string can contain a comma. Hence I've got a (now minor) problem...
Off to the testing,
Jettah
-
What version of Arexx are you running? I just used your code above and always got NUM values returned. I'm using 1.15.
-
adolescent wrote:
What version of Arexx are you running? I just used your code above and always got NUM values returned. I'm using 1.15.
Version of which file? :-?
I got the problem solved, but it tooka bit of weird thinking though... :-o
Regards,
Jettah
-
Version of ARexx itself. On my 1.15 system your code worked fine and always returned NUM.