convert variables types

kanikakanika Member Posts: 247
Hi!! I have 3 variables.
A (text, 7)
B (code)
C (decimal)
I need to do
A = B + C
but I don't know to convert types
Please, with function I need?
Thanks.

Answers

  • stiastastiasta Member Posts: 106
    First you can use a temp variable
    T (decimal)
    Then you run this code.
    A (text, 7)
    B (code)
    C (decimal)
    EVALUATE(T, B);
    A = FORMAT(T + C)
  • vijay_gvijay_g Member Posts: 884
    look at EVALUATE function in C/side reference guide.
  • FlorisB1984FlorisB1984 Member Posts: 17
    Hi Kanika,

    can you tell us what you are trying to do? Does var b contain a number or a number with text?
    Do ytou what to sum the var b and c?

    I'll add to code samples, perhaps that helps.
    1. Adding a number to a text:

    b:='PINV';
    c := 1.2;
    a := b + format(c);
    if you place this code in a codeunit and you add a message like this:
    message(a);
    the result wel be: PINV1.2

    2. Sum the number values:
    b := '5'
    c := 1.2;
    EVALUATE(d,b); //d is a var of the type decimal
    a := FORMAT(d + c);
    message(a) will result in 6.2

    Hope this helps,

    Floris
  • kanikakanika Member Posts: 247
    B = 0000002 (code)
    C = 2 (decimal)

    I need to get 0000004 in A (text,7) for OutFile.WRITE(A)

    THANK YOU VERY MUCH!!

    stiasta, your code generates this error: Type conversion is not possible because 1 of the operators contains an invalid type. Text + Decimal
  • FlorisB1984FlorisB1984 Member Posts: 17
    you're welcome :wink:

    please set topic as solved.

    regards,

    Floris
  • kanikakanika Member Posts: 247
    still does not work

    it is giving the same error
  • stiastastiasta Member Posts: 106
    is T a decimal and not text?
  • FlorisB1984FlorisB1984 Member Posts: 17
    Sorry Kanika,

    I assumed your issue was solved.

    According to the message, I guess you're trying to add a text value to a decimal. The other way around should not give errors.
    Please check that the value you wish to add is in the correct format.

    Floris
  • kanikakanika Member Posts: 247
    it already works!
    The problem was that B was in a Report DataItem and C in another.

    but the result is '4' and I need '0000004'

    I tried with padstr(string,7,0) but the result is '4000000' not '0000004'

    pleaseeeeeeeee, How do I do?
  • FlorisB1984FlorisB1984 Member Posts: 17
    what you could do is the following:

    IF STRLEN(a) <7 THEN
    REPEAT
    a:= '0' + a;
    UNTIL STRLEN(a) = 7;

    don't know if there is a better solution.....

    ---- EDIT ----
    What also works is:

    IF STRLEN(a) <7 THEN BEGIN
    textvar := PADSTR(textvar, 7-STRLEN(a), '0');
    a:= textvar + a;
    END;

    I suppose that's better for you performance
  • kanikakanika Member Posts: 247
    =D>
    Now It works perfectly!!

    thanks thanks! FlorisB

    How can I set topic as solved??
  • ProcatProcat Member Posts: 31
    And yet another formatting option:

    PADSTR('', 7-STRLEN(FORMAT(result)), '0')+FORMAT(result)

    There's probably better versions of this around.
  • vaprogvaprog Member Posts: 1,141
    For example
    FORMAT(Result,7,'<Integer,7><Filler Character,0>');
    
    (Why use format for conversion to standard format only? Format has parameters.
    This version cuts off the fractional part. You probably should deal with a potential fractional part with any formating option given here (or use (big)integer in the first place).
Sign In or Register to comment.