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.

IsNumeric

All good and well.  Working fine so far.  But if you can write each function with a single line, would that not be better ?

NewIsNumeric

Thanks for the inspiration Microsoft.

11 thoughts on “Inspired by a Microsoft Developer

  1. Peter Tijsma says:

    Hi Gunnar,
    How about this one?

    IsNumeric(CheckString : Text) : Boolean
    EXIT(EVALUATE(DummyDecimal,CheckString));

    Were “DummyDecimal” is a local variable of type “Decimal”

    😉

    1. EVALUATE(‘1.555’,DummyDecimal) will return TRUE, I don’t want that in this case.

  2. Chris B. says:

    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

    1. Then you just use EVALUATE

      1. Chris B. says:

        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.

        1. Then I would use Decimal.TryPharse with CultureInfo, see Codeunit 10 or the new Transformation functionality in the Data Exchange

  3. Mike Doster says:

    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: :”/\|?*’

  4. Kasper says:

    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);

    1. Kasper, look at the comment thread with Chris here above

  5. the one liners are back. great!

Leave a Reply

%d bloggers like this: