web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Numbers to text with currency X++

(2) ShareShare
ReportReport
Posted on by 124

Hello everyone,

Are there any available static methods in X++ that accepts a real and currency?

I want to convert $5.00 to Five Dollars And 00 Cents

Thanks in advance.

I have the same question (0)
  • Suggested answer
    ergun sahin Profile Picture
    8,826 Moderator on at

    There is a method written using numeralsToTxt in the localization, but I cannot share the code because I did not write it.

    You can write a custom method by using global::numeralsToTxt and numeralsToTxt_ES/FR/BR

  • Suggested answer
    nmaenpaa Profile Picture
    101,166 Moderator on at

    You can check Global class, it has some. But you might need to use the existing ones as example and develop your own.

    Global\numeralsToTxt_EN will convert the number to English, but doesn't support currency code.

    Global\numeralsToTxt_ES will convert the number to Spanish, and supports three currency codes.

    These should contain all that you need to develop your own method.

  • huijij Profile Picture
    19,811 on at

    As Ergün and Nikolaos mentioned, there is no such function. You might write your own logic referring to Global class for method of global::numeralsToTxt.

    regards

  • Verified answer
    Hossein.K Profile Picture
    6,648 on at

    Hi xppdev,

    Try 2 below useful link. In the two link, there are a sample function to does this.

    https://dynamicsuser.net/ax/f/technical/51154/how-to-convert-real-numbers-into-text 

    https://brotetechnologies.com/amount-conversion-with-currency-name-in-microsoft-dynamics-ax/ 

  • Priya_K Profile Picture
    186 on at
    Hello @xppdev,
     
    Use this code, it will work:
     
    Create a new class, add this method to that class, then use this method in your DP class- 
     
    Class totalinwords
    {
    static TempStr numeralsToTxt_USD(real _num)
    {
        int     numOfPennies = (decRound(frac(_num), 2) * 100) mod 100;
        real    amount         = _num - frac(_num);
        int     numOfTenths;
        str 20  ones[19], tenths[9], hundreds, thousands, millions, billions, trillions;

     

        int64   temp;
        str 200 returntxt;
        boolean checkStatus = false;

     

        str 200 pennytxt;// <change>
        int penny; //<change>
        #define.cents("cents")//<change>

     

        real modOperator(real a1, real a2)
        {
            int tmpi;
            real tmp1, tmp2;
            tmp1 = a1 / a2;
            tmpi = real2int(tmp1);
            tmp2 = tmpi;
            return (tmp1 - tmp2)*a2;
        }

     

        real checkPower(real  _amount, int64 _power)
        {
            int64   numOfPower;

     

            if (_amount >= _power)
            {
                numOfPower = real2int(_amount) div _power;
                if (numOfPower >= 100)
                {
                    temp = numOfPower div 100;
                    returntxt = returntxt + ' ' + ones[temp] + ' ' + hundreds;
                    numOfPower = numOfPower mod 100;
                }
                if (numOfPower >= 20)
                {
                    temp = numOfPower div 10;
                    returntxt = returntxt + ' ' + tenths[temp];
                    numOfPower = numOfPower mod 10;
                }
                if (numOfPower >= 1)
                {
                    returntxt = returntxt + ' ' + ones[numOfPower];
                    numOfPower = numOfPower mod 10;
                }
                switch(_power)
                {
                    case 1000000000000 :
                        returntxt = returntxt + ' ' + trillions;
                        _amount = modOperator(_amount, 1000000000000.00);
                        break;
                    case 1000000000 :
                        returntxt = returntxt + ' ' + billions;
                        _amount = modOperator(_amount, 1000000000);
                        break;
                    case 1000000 :
                        returntxt = returntxt + ' ' + millions;
                        _amount = modOperator(_amount, 1000000);
                        break;
                    case 1000 :
                        returntxt = returntxt + ' ' + thousands;
                        _amount = modOperator(_amount, 1000);
                        break;
                    case 100 :
                        returntxt = returntxt + ' ' + hundreds;
                        _amount = modOperator(_amount, 100);
                        break;
                }
            }
            return _amount;
        }

        #Define.text_1('One')
        #Define.text_2('Two')
        #Define.text_3('Three')
        #Define.text_4('Four')
        #Define.text_5('Five')
        #Define.text_6('Six')
        #Define.text_7('Seven')
        #Define.text_8('Eight')
        #Define.text_9('Nine')
        #Define.text_10('Ten')
        #Define.text_11('Eleven')
        #Define.text_12('Twelve')
        #Define.text_13('Thirteen')
        #Define.text_14('Fourteen')
        #Define.text_15('Fifteen')
        #Define.text_16('Sixteen')
        #Define.text_17('Seventeen')
        #Define.text_18('Eighteen')
        #Define.text_19('Nineteen')
        #Define.text_20('Twenty')
        #Define.text_30('Thirty')
        #Define.text_40('Forty')
        #Define.text_50('Fifty')
        #Define.text_60('Sixty')
        #Define.text_70('Seventy')
        #Define.text_80('Eighty')
        #Define.text_90('Ninety')
        #Define.text_100('Hundred')
        #Define.text_1000('Thousand')
        #Define.text_1000000('Million')
        #Define.text_1000000000('Billion')
        #Define.text_1000000000000('Trillion')
        #Define.text_and('Dollars and')
        #Define.text_cents('Cents Only')

        ones[1] = #text_1;
        ones[2] = #text_2;
        ones[3] = #text_3;
        ones[4] = #text_4;
        ones[5] = #text_5;
        ones[6] = #text_6;
        ones[7] = #text_7;
        ones[8] = #text_8;
        ones[9] = #text_9;
        ones[10] = #text_10;
        ones[11] = #text_11;
        ones[12] = #text_12;
        ones[13] = #text_13;
        ones[14] = #text_14;
        ones[15] = #text_15;
        ones[16] = #text_16;
        ones[17] = #text_17;
        ones[18] = #text_18;
        ones[19] = #text_19;

        tenths[1] = 'Not used';
        tenths[2] = #text_20;
        tenths[3] = #text_30;
        tenths[4] = #text_40;
        tenths[5] = #text_50;
        tenths[6] = #text_60;
        tenths[7] = #text_70;
        tenths[8] = #text_80;
        tenths[9] = #text_90;

        hundreds    = #text_100;
        thousands   = #text_1000;
        millions    = #text_1000000;
        billions    = #text_1000000000;
        trillions   = #text_1000000000000;

        amount = checkPower(amount, 1000000000000);
        amount = checkPower(amount, 1000000000);
        amount = checkPower(amount, 1000000);
        amount = checkPower(amount, 1000);
        amount = checkPower(amount, 100);

        if (amount >= 20)
        {
            numOfTenths = real2int(amount) div 10;
            returntxt = returntxt + ' ' + tenths[numofTenths];
            numOfTenths = numOfTenths mod 10;
            amount = real2int(amount) mod 10;
        }
        if (amount >= 1)
        {
            numOfTenths = real2int(amount);
            returntxt = returntxt + ' ' + ones[numOfTenths];
        }
        if(numOfPennies)
        {
            if (numOfPennies >= 20)
            {
                numOfTenths = numOfPennies div 10;
                returntxt =  returntxt + ' '+ #text_and  +' '+ tenths[numofTenths];
                numOfTenths = numOfTenths mod 10;
                numOfPennies = numOfPennies mod 10;
                checkStatus = true;
            }
            if (numOfPennies >= 1)
            {
                numOfTenths = real2int(numOfPennies);
                returntxt = checkStatus == true ? returntxt + ' ' + ones[numOfTenths] : returntxt + ' '+ #text_and +' ' + ones[numOfTenths];
                checkStatus = true;
            }
        }
        else
        {
            returntxt = returntxt + ' ' + #text_and +' '+"@SYS2068";
        }

        return   returntxt + ' ' + #text_cents;
    }

    }

     

    Call method like this: Table.Field = ClassName::MethodName(Varible);

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

News and Announcements

Season of Giving Solutions is Here!

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Abhilash Warrier Profile Picture

Abhilash Warrier 679 Super User 2025 Season 2

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 429 Super User 2025 Season 2

#3
Martin Dráb Profile Picture

Martin Dráb 264 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans