Thursday, 18 May 2017

Evaluating text value to decimal value based on the regional settings.

This is my first blog and i wish to take this as a platform to share my experience with MSD NAV. In this first post, i am sharing some basics about formatting the decimal text value before evaluating it to the decimal data type based on the regional settings. Hope this article is useful.

There are scenarios where we need to import / process a decimal text value to decimal field and it should work with different regional settings.

For example:
Decimal Text Value is 10.50 and the Regional Settings is using Comma as a decimal separator. In this case we need to convert the decimal separator in the text value to that one that is used in the regional settings before evaluating the decimal text value to decimal data type.

Below is the code for Converting the Decimal Text Value to Decimal Data Type:

OnRun()
MESSAGE('From Point Decimal Separator to Regional Settings (. --> ,) %1',ConvertTextToDecimal('10.50'));

MESSAGE('From Comma Decimal Separator to the Regional Settings (, --> ,) %1',ConvertTextToDecimal('10,50'));

    PROCEDURE ConvertTextToDecimal@1000000020(pDecText@1000000000 : Text) : Decimal;
    VAR
      lDecimal@1000000001 : Decimal;
    BEGIN
      //ConvertTextToDecimal
      IF pDecText = '' THEN
        EXIT(0);
      EVALUATE(lDecimal,FormatDecimal(pDecText));
      EXIT(lDecimal);
    END;

    PROCEDURE FormatDecimal@1000000009(pDecText@1000000000 : Text) : Text;
    VAR
      lDefaultDecimalSeparator@1000000001 : Text[2];
    BEGIN
      //FormatDecimal
      lDefaultDecimalSeparator := COPYSTR(FORMAT(1 / 2),2,1);
      CASE lDefaultDecimalSeparator OF
        '.': EXIT(CONVERTSTR(pDecText,',','.'));
        ',': EXIT(CONVERTSTR(pDecText,'.',','));
        ELSE
          EXIT(pDecText);
      END;
    END; 

Regional Settings:
  


Output after running the code unit:


Please do let me know your suggestion. Thanks

1 comment: