dinsdag 10 augustus 2010

num2str - decimals based on EDT

num2str function with the number of decimals based on the property of an Extended DataType

Code based on Mirko Bonello's work: http://dynamicsax-dev.blogspot.com/2009/02/getting-number-of-decimal-places-for.html

str num2strEdt(
    real _number,           //The real number to convert to a string
    int _character = 0,     //The minimum number of characters required in the text.
    ExtCodeValue _edt,      //The EDT to be used as basis for required number of decimal places.
    int _separator1 = 2,    //DecimalSeparator
    int _separator2 = 0)    //ThousandSeparator
    // http://www.rgagnon.com/pbdetails/pb-0181.html

    #WinAPI // Used for regional settings

    TreeNode treeNode;
    int decimalPlaces;
    treeNode = infolog.findNode(#ExtendedDataTypesPath + '\\' + _edt);
    if (!treeNode)
        return strfmt("%1", _number);

    if (findproperty(treeNode.AOTgetProperties(),#PropertyNoOfDecimals) == #AUTO)
      // get the number of decimals from the regional settings
      decimalPlaces = str2int((WinAPI::getLocaleInfo(#LOCALE_USER_DEFAULT,   #LOCALE_ICURRDIGITS)));
      // get the number of decimals set by the developer in the property inspector
      decimalPlaces = str2int(findproperty(treeNode.AOTgetProperties(),#PropertyNoOfDecimals));

    return num2str(_number, _character, decimalPlaces, _separator1, _separator2);

usage example:

info(strfmt("%1", num2strEdt(20.34, 0, (identifierstr(myEDT)))));

Geen opmerkingen:

Een reactie plaatsen