Click here to Skip to main content
Click here to Skip to main content

Convert Arabic Numbers or Digits to text

By , 18 May 2010
Rate this:
Please Sign up or sign in to vote.

Introduction


Many Day-to-Day business applications need to have the ability to convert numbers into words. In commercial, accounting or even auditing applications it is really a nice to have feature.

Background


Arabic numbers writing numbers is more complicated than English. In Arabic we have to read the hundreds first then ones after that tens. Unlike English which is more easier to read and it is one way.


There is also another way in Arabic to read numbers to begin from ones then tens then hundreds but this is not a very well-known method.

The code used here is considering the daily needs of accounting programs. Thus, it uses modern Arabic accounting language which may not be 100% compatible with Arabic grammar rules.

More efforts is needed if we are going to consider the gender of the count, the two cases and the morphology analysis which is out of the scope of the current code snippet.

Using the Code


The main entry of the Class NumberingFormatter is the method getResultEnhanced().

This method is taking care of the Arabic number string entered and analyze whether the string is millions or hundreds or thousands. Based on these rules, the string is submitted to different methods depends on the nature of the String.
Collapse

public string getResultEnhanced()
        {
            String result = "";
            if (Number.Length % 3 == 1)
                Number = "00" + Number;
            else if (Number.Length % 3 == 2)
                Number = "0" + Number;
            
            
            if (Number.Length == 3)
                result = getThreeNumberCase(Number);
            else if (Number.Length == 6)
            {
                result = getSixNumberCase(Number);
            }
            else if (Number.Length == 9)
            {
                result = getNineNumberCase(Number);
            }
            result = result.Replace("  ", " ");
            result = result.Replace("   ", " ");
            result = result.Replace(" و ريال", " ريال ");
            
            return result;
        }

Other than this there is only a structure of methods to deal with words and numbering.

Of course, the parts of the numbers are stored in static variable like tens and hundreds, and then composed up to build the final text.

Please notice that the algorithm doesn't have any defensive approach against the wrong input like letters or 000005 because of simplicity and separation of concerns. It is assumed that the front-end consumer or the controller will do the input sanitation check.

Points of Interest


There was a big discussion about million number is it Arabic or not. But the clear fact is that Million مليون doesn't have Arabic root. The algorithm dealt with millions in a different way than thousands or hundreds.

License

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

About the Author

xprog

Saudi Arabia Saudi Arabia
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 18 May 2010
Article Copyright 2010 by xprog
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid