Skip to main content

Notifications

Business Central forum
Suggested answer

Convert Number to Words in Business Central

Posted on by 71
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;
 
 
}
Categories:
  • Suggested answer
    YUN ZHU Profile Picture
    YUN ZHU 62,529 Super User 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 12,769 User Group Leader 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 4,046 Super User on at
    Convert Number to Words in Business Central

Helpful resources

Quick Links

Community Spotlight of the Month

Kudos to Mohamed Amine Mahmoudi!

Blog subscriptions now enabled!

Follow your favorite blogs

TechTalk: How Dataverse and Microsoft Fabric powers ...

Explore the latest advancements in data export and integration within ...

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 284,217 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,202 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,146

Featured topics

Product updates

Dynamics 365 release plans