Skip to main content

Notifications

Small and medium business | Business Central, N...
Suggested answer

Convert Number to Words in Business Central

(1) ShareShare
ReportReport
Posted on by 80
Hi All,
I've written an action to convert a number to words using a codeunit, but I'm encountering an /Index Out of Bounds/ error. I am not sure why this issue is occurring and would greatly appreciate any assistance or solutions you can provide.
For example, 5000 -> 5 thousand.
 
 
page 50323 CustomerInfo
{
    ApplicationArea = All;
    Caption = 'CustomerInfo';
    PageType = List;
    SourceTable = Customer;
    UsageCategory = Lists;
    //InsertAllowed = true;
    //Permissions = tabledata Customer = R;
    InsertAllowed = true;
    DeleteAllowed = true;
    ModifyAllowed = true;
 
   
 
    actions
    {
        area(Processing)
        {
            group(Home)
            {
                action(/Get Number to words/)
                {
                    trigger OnAction()
                    var
                        myInt: Integer;
                        Words: Text;
                    begin
                        myInt := 45;
                        Words := ConvertNumberToWords(myInt);
                        MESSAGE('Number in Words: %1', Words);
                    end;
 
                }
            }
        }
    }
 
    local procedure AmountToWords(Amount: Integer): Text
    var
        Word: Text;
    begin
        Word := FormatAmountInWords(Amount);
        exit(Word);
    end;
 
    local procedure FormatAmountInWords(Amount: Integer): Text
    var
        WholePart: Integer;
        FractionPart: Integer;
        Words: Text;
    begin
        WholePart := Amount;
        //FractionPart := Round((Amount - WholePart) * 100);
 
        Words := ConvertNumberToWords(WholePart) + ' Rupees';
        // if FractionPart > 0 then
        //     Words := Words + ' and ' + ConvertNumberToWords(FractionPart) + ' Paise';
 
        exit(Words);
    end;
 
    local procedure ConvertNumberToWords(Number: Integer): Text
    var
        Units: array[19] of Text[10];
        Tens: array[10] of Text[10];
        Hundreds: array[5] of Text[10];
        Result: Text;
        HValue: Integer;
        TValue: Integer;
    begin
       
        Units[0] := 'Zero';
        Units[1] := 'One';
        Units[2] := 'Two';
        Units[3] := 'Three';
        Units[4] := 'Four';
        Units[5] := 'Five';
        Units[6] := 'Six';
        Units[7] := 'Seven';
        Units[8] := 'Eight';
        Units[9] := 'Nine';
        Units[10] := 'Ten';
        Units[11] := 'Eleven';
        Units[12] := 'Twelve';
        Units[13] := 'Thirteen';
        Units[14] := 'Fourteen';
        Units[15] := 'Fifteen';
        Units[16] := 'Sixteen';
        Units[17] := 'Seventeen';
        Units[18] := 'Eighteen';
        Units[19] := 'Nineteen';
 
        Tens[2] := 'Twenty';
        Tens[3] := 'Thirty';
        Tens[4] := 'Forty';
        Tens[5] := 'Fifty';
        Tens[6] := 'Sixty';
        Tens[7] := 'Seventy';
        Tens[8] := 'Eighty';
        Tens[9] := 'Ninety';
 
        if Number < 20 then
            Result := Units[Number]
        else if (Number < 100) and (Number > 19) then
            Result := Tens[Number DIV 10] + ' ' + Units[Number MOD 10]
        else if (Number > 100) and (Number < 999) then
            HValue := Number MOD 100;
            Result := Units[Number DIV 100] + 'Hundred' + ConvertNumberToWords(HValue);
        if (Number > 999) and (Number < 9999) then
            TValue := Number MOD 1000;
            HValue := HValue MOD 100;
            Result := Units[Number DIV 1000] + 'Thousand' + ConvertNumberToWords(TValue) + ConvertNumberToWords(HValue);
 
        exit(Result);
 
    end;
 
 
}
  • Suggested answer
    YUN ZHU Profile Picture
    YUN ZHU 74,036 Super User 2024 Season 2 on at
    Convert Number to Words in Business Central
    Hi, you can also try the standard method below.
    report 1401 Check
     
    Thanks
    ZHU
  • Suggested answer
    Saurav.Dhyani Profile Picture
    Saurav.Dhyani 17,965 Super User 2024 Season 2 on at
    Convert Number to Words in Business Central
    Hi,
     
    Don't create custom code for it.
     
    Refer Report 1401 and check how Microsoft is using FormatNoText method to convert amount in words.
     
    The function actual is placed in report 10400 "Check Translation Management"
     
    Regards,
    Saurav Dhyani
  • Suggested answer
    gdrenteria Profile Picture
    gdrenteria 12,290 Most Valuable Professional on at
    Convert Number to Words in Business Central

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

Announcing Category Subscriptions!

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,359 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,370 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans