Yes it happens.
When reading the C/AL code in NAV written by other developers you normally pick up smart way to do things. Yesterday I got one.
In my solutions I have been using a function to check if a string is numeric and another function to extract the numeric value from a string.
All good and well. Working fine so far. But if you can write each function with a single line, would that not be better ?
Thanks for the inspiration Microsoft.
Hi Gunnar,
How about this one?
IsNumeric(CheckString : Text) : Boolean
EXIT(EVALUATE(DummyDecimal,CheckString));
Were “DummyDecimal” is a local variable of type “Decimal”
đŸ˜‰
EVALUATE(‘1.555’,DummyDecimal) will return TRUE, I don’t want that in this case.
Hi,
imho good but not perfect. think about decimals and seperators. 1,234.56 would’nt match.
Maybe this whould be better:
[…]DECHR(CheckString,’=’,’1234567890,.’)[…]
Greets
Chris
Then you just use EVALUATE
The Problem wirh evaluate is the using of a specific culture
e.g. 1’234.56 is a valid number in switzerland but would fail in an evaluate on a en-us Server configuration.
Then I would use Decimal.TryPharse with CultureInfo, see Codeunit 10 or the new Transformation functionality in the Data Exchange
This is a good one, Gunnar. I stumbled into this while cleaning illegal characters from file names I was creating from mixed input. A slightly different implementation using a text constant.
ObjectFileName := DELCHR(Object.Name,’=’,Text007)), 1, MAXSTRLEN(ObjectFileName));
Where Text007 is all the illegal characters: :”/|?*’
IsNumeric will return FALSE if the CheckString is ‘1.234’ or ‘2,00’.
Normally I would do it like this:
IF EVALUATE(DecimalValue,CheckString) THEN
EXIT((DecimalValue MOD 1) = 0)
ELSE
EXIT(FALSE);
Kasper, look at the comment thread with Chris here above
the one liners are back. great!