How to get the decimal value alone correctly?

Aravindh_NavisionAravindh_Navision Member Posts: 258
Hi Friends,

I have a decimal field. I have to seperate the integer part and decimal part seperately and store in string field.

Decimal Field Value: 62134.1818515

The output I expect is:
IntegerPart : 62134
DecimalPart : 18

Note: I need not round this to any position.

Datatype: IntegerPart, DecimalPart - Code[20]

The code I wrote is:
IntegerPart := FORMAT(DecimalFieldValue,0,'<Integer>');
DecimalPart:= FORMAT(DecimalFieldValue,0,'<Decimals,2>');

I am getting the output as: 62134**

Thats is IntegerPart : 62134
DecimalPart : **

Why it is so? Can anyone help me on this?

Thanks in advance
Aravindh R.

Comments

  • einsTeIn.NETeinsTeIn.NET Member Posts: 1,050
    "Money is likewise the greatest chance and the greatest scourge of mankind."
  • ChinmoyChinmoy Member Posts: 359
    You can try this:

    DecimalPart:= FORMAT(DecimalFieldValue,0,'<Decimals,8>');
    DecimalPart := COPYSTR(DecimalPart,2,2);
  • KYDutchieKYDutchie Member Posts: 345
    Hi,

    how about this:

    decimal := 62134.1818515;
    integerpart := FORMAT(decimal,0,'<Integer>');
    decimalpart := COPYSTR(FORMAT(decimal,0,'<Decimals>'),2,STRLEN(FORMAT(decimal,0,'<Decimals>')));
    
    MESSAGE('Int: %1  dec: %2',integerpart,decimalpart);
    

    Will this work for you?

    Regards,

    Willy
    Fostering a homeless, abused child is the hardest yet most rewarding thing I have ever done.
  • vaprogvaprog Member Posts: 1,141
    The decimal part of your number is 8 characters long. Because this does not fit into your given field length of 2, the whole field is filled with *. You need to cut out the needed length afterwards. There is another problem, though. The Decimals part contains the decimal separator as it's first character. I suggest to use
    DecimalPart := DELCHR(FORMAT(DecimalFieldValue,3,'<Precision,2:><Decimals><Comma, >'));
    

    The <Precision,2:> part is to assure you get digits and no blanks within your two needed characters (for values of DecimalFieldValue with one or zero digits) This would otherwise lead to probably unexpected results: 3.1 -> '1' (or '1 '); 3.101 -> '10'. If you want to get blanks instead e.g. for 3.1 DecimalPart = '1 ', then you need to leave out that part of the format string and probably change DELCHR to only delete blanks at the beginning of the string and maybe add another DELCHR to delete '0' from the end.
  • einsTeIn.NETeinsTeIn.NET Member Posts: 1,050
    That's why I said ROUND.

    Simple solution if you don't have to consider anything else:
    dec := 62134.1818515;
    intpart := FORMAT(dec DIV 1);
    decpart := FORMAT(ROUND(dec) MOD 1 * 100);
    // OR: decpart := FORMAT((dec MOD 1 * 100) DIV 1);
    

    Complex solution:
    dec := -62134.188515;
    intpart := FORMAT(ABS(dec) DIV 1);
    decpart := FORMAT(ROUND(ABS(dec),0.01,'<') MOD 1 * 100);
    

    Both not tested...
    "Money is likewise the greatest chance and the greatest scourge of mankind."
Sign In or Register to comment.