Click here to Skip to main content
11,484,402 members (65,737 online)
Click here to Skip to main content

Number To Word (Arabic Version)

, 21 Mar 2012 CPOL 85.9K 4.1K 41
Rate this:
Please Sign up or sign in to vote.
An article that describes how to convert a number to written words in English and Arabic

Introduction  

In this article, I will talk about converting numbers to written words in English & Arabic (Tafqeet as pronounced in Arabic).

Background

I was in the middle of programming my project when I needed a code to convert a number to written text.

Our project is mulltilingual, so I needed two versions: one for English and one for Arabic.

I googled “Number to Text” or “Number to Word” and I found a lot of code to convert number to written words for English language and many of them worked fine.

I also found code to convert number to words for Arabic language, but unfortunately many of them didn’t work as expected!

So I have decided to go the long way to build a class to do the conversion to Arabic & also added English language support.

Here is a screen shot of a test project that uses the class:

Tafqeet2.jpg

Using the Code

I have to mention that I used code written by Justin Rogers from this link as a start: http://weblogs.asp.net/justin_rogers/articles/151757.aspx

This article contains code that does a good convert for a decimal value into its equivalent English words, but numbers from my application were actually money amounts, so I needed to add currency names to it (our project is also multinational, so I had to build the string dynamically from the country’s currency).

This was the easy part after I made a few small changes to Justin’s code to fulfill my English-Number-To-Words requirements.

Now comes the complicated part: converting numbers to written words in Arabic language!

Arabic language is very complicated, and when it comes to converting it to words, I think it is the most difficult language because it has so many rules that depend on the “number” state and the “counted” state.

For example:

The number one & two matches the counted in its feminine state, where the numbers from 3 to 10 are contrary to the counted, etc.

So I have used one method to determine a digit feminine status using its group level & Currency Name Feminine field status. Here is the method:

private string GetDigitFeminineStatus(int digit, int groupLevel)
{
    if (groupLevel == -1)
    { // if it is in the decimal part
        if (Currency.IsCurrencyPartNameFeminine)
            return arabicFeminineOnes[digit]; // use feminine field
        else
            return arabicOnes[digit];
    }
    else
        if (groupLevel == 0)
        { 
            if (Currency.IsCurrencyNameFeminine)
                return arabicFeminineOnes[digit];// use feminine field
            else
                return arabicOnes[digit];
        }
        else
            return arabicOnes[digit];
}

The whole convert process depends on dividing the number into Group Levels. Each group contains 3 digits & these levels are numbered as in this example:

Number: 987,654,321.345
345: Group Level -1
321: Group Level 0 
654: Group Level 1
987: Group Level 2 

This is accomplished by this code:

Byte group = 0;
while (tempNumber >= 1)
{
    // separate number into groups
    int numberToProcess = (int)(tempNumber % 1000);
    
    tempNumber = tempNumber / 1000;
    
    // convert group into its text
    string groupDescription = ProcessArabicGroup
    (numberToProcess, group, Math.Floor(tempNumber));
    
    group++;
}

Then we have to process each group & convert it to its text depending on its group level.

Inside the ProcesArabicGroup method, we check for the special case for 200 as it has its special rule in Arabic:

if (hundreds > 0)
{
     if (tens == 0 && hundreds == 2) // ???? ??????
         retVal = String.Format("{0}", arabicAppendedTwos[0]);
     else //  ?????? ???????
         retVal = String.Format("{0}", arabicHundreds[hundreds]);
} 

Also the number 2 has another special rule which is discussed by this code:

if (tens == 2 && hundreds == 0 && groupLevel > 0)
{ // This is special case for number 2 when it comes alone in the group
    if (_intergerValue == 2000 || _intergerValue == 2000000 || 
    _intergerValue == 2000000000 || _intergerValue == 2000000000000 || 
    _intergerValue == 2000000000000000 || _intergerValue == 2000000000000000000)
        retVal = String.Format("{0}", arabicAppendedTwos[groupLevel]); // ?? ???? ???????
    else
        retVal = String.Format("{0}", arabicTwos[groupLevel]);//  ?? ???? ???????
}

For group numbers over 20, it is considered as complicated of 2 digits so each one will get its own converted text.

int ones = tens % 10;
tens = (tens / 10) - 2; // 20's offset
 
if (ones > 0)
{
    if (retVal != String.Empty)
        retVal += " ? ";
 
    // Get Feminine status for this digit
    retVal += GetDigitFeminineStatus(ones, groupLevel);
}
 
if (retVal != String.Empty)
    retVal += " ? ";
 
// Get Tens text
retVal += arabicTens[tens];

Also Arabic language is sensitive to currency names which also depends on the number state so I had to add four different names for the same currency name to support all states. Their fields are used at the end of the method when constructing the final String:

if (_intergerValue != 0)
{ // here we add currency name depending on _intergerValue : 1 ,2 , 3--->10 , 11--->99
    int remaining100 = (int)(_intergerValue % 100);
    
    if (remaining100 == 0)
        formattedNumber += Currency.Arabic1CurrencyName;
    else
        if (remaining100 == 1)
            formattedNumber += Currency.Arabic1CurrencyName;
        else
            if (remaining100 == 2)
            {
                if (_intergerValue == 2)
                    formattedNumber += Currency.Arabic2CurrencyName;
                else
                    formattedNumber += Currency.Arabic1CurrencyName;
            }
            else
                if (remaining100 >= 3 && remaining100 <= 10)
                    formattedNumber += Currency.Arabic310CurrencyName;
                else
                    if (remaining100 >= 11 && remaining100 <= 99)
                        formattedNumber += Currency.Arabic1199CurrencyName;

I have created one class to simplify the process, it is called CurrencyInfo which contains the definition for Currency object. It has the following properties:

CurrencyID = 0;

Just ID

CurrencyCode = "SYP";

Its Code

IsCurrencyNameFeminine = true;

Is the currency name feminine or no?

EnglishCurrencyName = "Syrian Pound";

Like: one Syrian Pound

EnglishPluralCurrencyName = "Syrian Pounds";

Like: thirty four Syrian Pounds

EnglishCurrencyPartName = "Piaster";

For the decimal part

EnglishPluralCurrencyPartName = "Piasteres";

For the decimal part

Arabic1CurrencyName = "???? ?????";

For the number one in Arabic

Arabic2CurrencyName = "?????? ???????";

For the number two in Arabic

Arabic310CurrencyName = "????? ?????";

For numbers between 3 and 10 in Arabic

Arabic1199CurrencyName = "???? ?????";

For numbers above 11 in Arabic

Arabic1CurrencyPartName = "???";

For the decimal part in Arabic

Arabic2CurrencyPartName = "?????";

For the decimal part in Arabic

Arabic310CurrencyPartName = "????";

For the decimal part in Arabic

Arabic1199CurrencyPartName = "?????";

For the decimal part in Arabic

PartPrecision = 2;

Part precision, like: 1 Syrian Pound = 100 Piasters ( 2 is number of Zeros)

IsCurrencyPartNameFeminine

Is the currency part name feminine or no?

As you can see from the table, it is very easy to setup any other currency by filling the specified fields.

Points of Interest

I hope this was helpful to you.

Please feel free to correct/suggest any modifications to the code.

History

  • 27th September, 2010: Initial post
  • 9th April, 2011: Fixed a small issue (noted by one member) in the solution attached to the article
  • 5th May, 2011: Added VB.NET equivalent code for the article
  • 17th February, 2012: Fixed a small bug in the code

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Adel Khayata
Software Developer (Senior) Taiba
Syrian Arab Republic Syrian Arab Republic
MCSE,MCSD,MCTS,MCITP
Working as I.T Manager for Taiba Gold & Jewels (http://khayata.org)
Follow on   Twitter

Comments and Discussions

 
QuestionUsing on Microsoft Windows 7 platform? Pin
Member 1108033113-Sep-14 11:18
memberMember 1108033113-Sep-14 11:18 
QuestionAdd Currency Pin
Member 1091292829-Jun-14 14:02
memberMember 1091292829-Jun-14 14:02 
QuestionHow to set up the CurrencyInfo for Qatar and Koweit Pin
hemigueti21-Nov-13 4:22
memberhemigueti21-Nov-13 4:22 
AnswerRe: How to set up the CurrencyInfo for Qatar and Koweit Pin
Adel Khayata21-Nov-13 18:43
memberAdel Khayata21-Nov-13 18:43 
QuestionThank you Pin
Member 852104813-Nov-13 11:50
memberMember 852104813-Nov-13 11:50 
AnswerRe: Thank you Pin
Adel Khayata21-Nov-13 18:45
memberAdel Khayata21-Nov-13 18:45 
Questionnumber to word Pin
m-a-abudrais22-Mar-13 23:10
memberm-a-abudrais22-Mar-13 23:10 
AnswerRe: number to word Pin
Adel Khayata23-Mar-13 7:59
memberAdel Khayata23-Mar-13 7:59 
Questionconfig Pin
rasel20207816-Nov-12 7:43
memberrasel20207816-Nov-12 7:43 
AnswerRe: config Pin
Adel Khayata18-Nov-12 5:45
memberAdel Khayata18-Nov-12 5:45 
QuestionNumber To Word [modified] Pin
mustafa salman5-May-12 21:52
membermustafa salman5-May-12 21:52 
AnswerRe: Number To Word Pin
Adel Khayata6-May-12 3:57
memberAdel Khayata6-May-12 3:57 
GeneralRe: Number To Word Pin
mustafa salman7-May-12 23:56
membermustafa salman7-May-12 23:56 
GeneralGood Job Adel Pin
Aamer Alduais22-Mar-12 5:14
memberAamer Alduais22-Mar-12 5:14 
GeneralRe: Good Job Adel Pin
Adel Khayata22-Mar-12 6:31
memberAdel Khayata22-Mar-12 6:31 
Generalيعطيك العافية عالمجهود Pin
Mazen el Senih20-Mar-12 11:25
memberMazen el Senih20-Mar-12 11:25 
GeneralRe: يعطيك العافية عالمجهود Pin
Adel Khayata21-Mar-12 1:41
memberAdel Khayata21-Mar-12 1:41 
QuestionNumberToArabic code gave a wrong translation for the number 101000 [modified] Pin
Member 873607919-Mar-12 7:02
memberMember 873607919-Mar-12 7:02 
AnswerRe: NumberToArabic code gave a wrong translation for the number 101000 Pin
Adel Khayata19-Mar-12 8:18
memberAdel Khayata19-Mar-12 8:18 
GeneralRe: NumberToArabic code gave a wrong translation for the number 101000 Pin
Member 873607919-Mar-12 8:50
memberMember 873607919-Mar-12 8:50 
GeneralRe: NumberToArabic code gave a wrong translation for the number 101000 Pin
Member 873607919-Mar-12 9:19
memberMember 873607919-Mar-12 9:19 
GeneralRe: NumberToArabic code gave a wrong translation for the number 101000 Pin
Adel Khayata19-Mar-12 20:06
memberAdel Khayata19-Mar-12 20:06 
GeneralMy vote of 3 Pin
Tammam Koujan21-Feb-12 23:43
memberTammam Koujan21-Feb-12 23:43 
QuestionMy vote is 5 Pin
muhamd yusuf20-Feb-12 21:01
membermuhamd yusuf20-Feb-12 21:01 
AnswerRe: My vote is 5 Pin
Adel Khayata20-Feb-12 23:44
memberAdel Khayata20-Feb-12 23:44 
QuestionFix if there is one Number after decimal pionts Pin
Smart Arab17-Feb-12 2:23
memberSmart Arab17-Feb-12 2:23 
AnswerRe: Fix if there is one Number after decimal pionts Pin
Adel Khayata17-Feb-12 4:32
memberAdel Khayata17-Feb-12 4:32 
QuestionWonderful! Pin
Abdo W Achkar7-Feb-12 1:52
memberAbdo W Achkar7-Feb-12 1:52 
GeneralNice job Pin
NewPast.Net6-Jan-12 21:05
groupNewPast.Net6-Jan-12 21:05 
QuestionJava version Pin
Member 79926595-Jul-11 0:04
memberMember 79926595-Jul-11 0:04 
AnswerRe: Java version Pin
Adel Khayata5-Jul-11 0:32
memberAdel Khayata5-Jul-11 0:32 
GeneralRe: Java version Pin
Member 79926595-Jul-11 8:31
memberMember 79926595-Jul-11 8:31 
GeneralRe: Java version Pin
Adel Khayata5-Jul-11 20:16
memberAdel Khayata5-Jul-11 20:16 
AnswerRe: Java version Pin
crashenburne13-Oct-11 21:24
membercrashenburne13-Oct-11 21:24 
GeneralFixed Pin
rogermacosta2-Jul-11 8:27
memberrogermacosta2-Jul-11 8:27 
BugVB code Pin
rogermacosta2-Jul-11 7:58
memberrogermacosta2-Jul-11 7:58 
Generalvery usefl and to the point [modified] Pin
Omar Shraim9-Apr-11 2:40
memberOmar Shraim9-Apr-11 2:40 
GeneralRe: very usefl and to the point Pin
Adel Khayata11-Apr-11 0:40
memberAdel Khayata11-Apr-11 0:40 
GeneralNumber to Word - Arabic Pin
Member 229774129-Mar-11 21:21
memberMember 229774129-Mar-11 21:21 
GeneralRe: Number to Word - Arabic Pin
Adel Khayata29-Mar-11 22:06
memberAdel Khayata29-Mar-11 22:06 
GeneralRe: Number to Word - Arabic Pin
Member 22977417-Apr-11 0:01
memberMember 22977417-Apr-11 0:01 
GeneralRe: Number to Word - Arabic Pin
Adel Khayata7-Apr-11 0:04
memberAdel Khayata7-Apr-11 0:04 
GeneralRe: Number to Word - Arabic Pin
nazar121-May-11 7:48
membernazar121-May-11 7:48 
GeneralRe: Number to Word - Arabic Pin
Adel Khayata6-May-11 7:18
memberAdel Khayata6-May-11 7:18 
AnswerRe: Number to Word - Arabic Pin
thatraja26-Jan-12 22:10
mvpthatraja26-Jan-12 22:10 
GeneralRe: Number to Word - Arabic Pin
Adel Khayata26-Jan-12 23:28
memberAdel Khayata26-Jan-12 23:28 
GeneralMy vote of 5 Pin
Nacereddine14-Nov-10 6:55
memberNacereddine14-Nov-10 6:55 
GeneralMy vote of 5 Pin
harafeh5-Oct-10 7:43
memberharafeh5-Oct-10 7:43 
GeneralExcellent Pin
mhn21727-Sep-10 19:01
membermhn21727-Sep-10 19:01 
Generalthanks Pin
jahmani27-Sep-10 15:11
memberjahmani27-Sep-10 15:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150520.1 | Last Updated 21 Mar 2012
Article Copyright 2010 by Adel Khayata
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid