Displaying Arabic Number.

 Well, most applications that I worked with was multilingual that supports English UI and Arabic UI.

 And one of the major issue that we have faced is displaying Arabic numbers without the need of changing the regional settings of the PC.

So the code below will help you to display Arabic number without  changing any regional settings.


Code 1: the HTML code:

 

<form id="form1" runat="server">

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

<br />

<asp:Label ID="Label1" runat="server"></asp:Label>

<br />

<asp:Button ID="Button1" runat="server" Text="Convert to Arabic" />

</form>

 

Code 2: Code Behind

 

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

 

        'Call Function

        Me.Label1.Text = "Arabic Number : <b>" & TranslateNumerals(Me.TextBox1.Text.Trim) & "</b>"

    End Sub

 

 

    Public Shared Function TranslateNumerals(ByVal sIn As String) As String

 

        Dim enc As New System.Text.UTF8Encoding

 

        Dim utf8Decoder As System.Text.Decoder

 

        utf8Decoder = enc.GetDecoder

 

        Dim sTranslated = New System.Text.StringBuilder

 

        Dim cTransChar(1) As Char

 

        Dim bytes() As Byte = {217, 160}

 

        ' Start Converting characters into Arabic mode.

 

        Dim aChars() As Char = sIn.ToCharArray

 

        For Each c As Char In aChars

 

            If Char.IsDigit(c) Then

 

                bytes(1) = 160 + CInt(Char.GetNumericValue(c))

 

                utf8Decoder.GetChars(bytes, 0, 2, cTransChar, 0)

 

                sTranslated.Append(cTransChar(0))

 

            Else

 

                sTranslated.Append(c)

 

            End If

 

        Next

 

        TranslateNumerals = sTranslated.ToString

 

    End Function

 

 

After you run the page, enter some English number and then click "Convert to Arabic" button to display the entered number in Arabic number mode.

Figure 1: Convert English Number into Arabic Number

 

 

I hope some one will find this very helpful

 ~ Abdulla AbdelHaq

103 Comments

  • Hi Abdulla,

    I read this post this morning at it made me realize that my CMS mojoPortal is not really localizing everything as I thought. I always thought that calling .ToString() on an int or decimal would automaticaly format it according to the CultureInfo of the executing thread. I mean it does format it but it doesn't localize the digits. To me it seems like a shortcoming of ASP.NET localization that it does not do this automatically.
    I thought there must be some way to make it work so I spent the afternoon doing my own investigation and trying to find a way to make it just work with .ToString()

    I hoped it would work like this:
    if ((CultureInfo.CurrentCulture.TwoLetterISOLanguageName == "ar") && (!CultureInfo.CurrentCulture.IsNeutralCulture))
    {
    CultureInfo arabic = new CultureInfo(CultureInfo.CurrentCulture.Name);
    arabic.NumberFormat.DigitSubstitution = DigitShapes.NativeNational;
    string[] arabicDigits = new string[] { "٠", "١", "٢", "٣", "٤", "٥", "٦", "٧", "٨", "٩" };
    arabic.NumberFormat.NativeDigits = arabicDigits;
    Thread.CurrentThread.CurrentCulture = arabic;
    Thread.CurrentThread.CurrentUICulture = arabic;
    }

    I tried all the DigitSubstitution Enums but no matter what I tried I could not make it work. It seems to really be limited to the machine's region settings.

    So your solution seems the only real way to do it.

    I converted your method to C# SubstituteArabicDigits(string input) and then created an extension method for int so I can call .ToLocalString() on any int and internally use your method like this if the culture is Arabic

    public static string ToLocalString(this int i)
    {
    switch (CultureInfo.CurrentCulture.TwoLetterISOLanguageName)
    {
    case "ar":
    return SubstituteArabicDigits(i.ToString());

    default:
    return i.ToString();

    }

    }

    I'm thinking I can implement similar solutions for other languages based on your example and extension methods for decimal, double etc.

    So thanks for sharing this strategy and pointing out that digit localization doesn't just happen automatically in ASP.NET.

  • Hi,

    I need convert English text to Arabic in VB.net

    any idea please

  • Hello Saravanan,
    You can localize your text to display the proper language to you page depending on the current UI culture.
    the below article is a good start to learn localization
    http://aspalliance.com/821_Localization_in_ASPNET_20

    feel free to contact me if you still have a problem or you start a topic on the ASP.NET official forum and I will be there to assist you.

  • joeaudette could you post your C# method? thanks

  • nevermind joeaudette. here is my C# version just in case anyone needs it.

    public string ConvertToArabicNumerals(string input)
    {
    System.Text.UTF8Encoding utf8Encoder = new UTF8Encoding();
    System.Text.Decoder utf8Decoder = utf8Encoder.GetDecoder();
    System.Text.StringBuilder convertedChars = new System.Text.StringBuilder();
    char[] convertedChar = new char[1];
    byte[] bytes = new byte[]{217,160};
    char[] inputCharArray = input.ToCharArray();
    foreach (char c in inputCharArray)
    {
    if(char.IsDigit(c))
    {
    bytes[1] = Convert.ToByte(160 + char.GetNumericValue(c));
    utf8Decoder.GetChars(bytes, 0, 2, convertedChar, 0);
    convertedChars.Append(convertedChar[0]);
    }
    else
    {
    convertedChars.Append(c);
    }
    }
    return convertedChars.ToString();
    }

  • Re: nick

    there is many free sites that helps you to convert between C# and VB check this

    http://www.developerfusion.com/tools/convert/vb-to-csharp/

  • Adullah, thanks for the link. Do you know how to test if a string is in Arabic? do you have to test the individual characters to see if they fall within the arabic character set or is there an easier way to do it. thanks

  • Re: Nick

    Hello,
    Yes you can. you can check the ASCII code if its in the arabic range or not.
    Convert the VB code to C#.

    Shared Function FindArabicChr(ByVal _Text As String) As Boolean

    Dim Counter As Long
    Dim srcLength As Long = Len(_Text) - 1
    Dim srcText As String = _Text
    Dim flg As Boolean = False

    Do While Counter = 153 And (Asc(srcText.Chars(Counter)) <= 254)) Then
    flg = True
    Exit Do
    End If

    Counter += 1
    Loop

    return flg

    End Function

  • Great! thanks for your help Abdullah

  • You are welcome :)
    You know, I should post the above code in a new post, maybe it could help some one else.

  • can you pleaqse give me the sample code to convert a string into arabic language:

    For example:

    string str="this string will be in arabic";

    i want to convert it into arabic....
    can u please help me?

  • Re: Kaleem Anwar

    Hello Kaleem,
    you want a translator !
    another solution you can go through with is to localize your text
    read this
    http://aspalliance.com/821_Localization_in_ASPNET_20

  • thanks for the reply but through localization i can only translate static text i want to translate text dynamic that will come from database

  • easy, you just make a column for each language in DB.
    for example if you have a user table that contains username, then you need to create two column for username one for english and the other for arabic, and make sure that you use Nvarchar for arabic column.
    now in UI. you get the proper column depend on UI culture.

  • Ass salam alikom,hi abdullah i am developing a website,n i have to develop the website both in arabic in english,so how can i change the gridview colums into arabic as the gridview is attached to sqldatasource,so can u help me out,i am developing the website in vb bro so can forward me some code

  • Re: Imran

    Hello Imran,

    I answered your question on ASP.NET forum, I found that you have asked the same question in there.

    http://forums.asp.net/p/1470460/3404638.aspx#3404638

  • Has anyone got a version of this function that reverses the job, translates the Arabic numbers into English?

  • Re:Islam Eldemery

    Well, you can ask who wrote the above function :)

  • Hi Abdullah, Well I thought this post was little old and didn't expect you to reply, Actually you did a great job with this method, I'm sorry I should have asked you!

    So since you replied, Do you have any idea how to get this function reversed?

  • Re:Islam Eldemery
    Hi, I am just kidding with you :)
    I receive an email when anyone submits a comment.
    Anyway, you do not need to reveres numbers from Arabic to English, because the default page culture will understand any number as English number, so if you get a sets of numbers from DB and you want to display them into UI, then you can display it as is.
    But if you want to display Arabic numbers, then you need to use the above method, because UI does not understand Arabic number.
    Did you got it ?

  • Hi Abdullah,
    Your posting was really helpfull....

    can we type arabic numerals in a textbox in vb6? if yes, plz explain..
    I changed locale settings in xp and set digit substitution to "context" in regional settings..but still can't type. Plz help..

    Thanks & Regards

  • Re: Arun

    Hi,
    Sorry for my late response, I was so busy last few days.
    Actually, last time I worked with VB6 where in University 2004 Loooool.

    I totally forget VB6. But do you have Arabic language installed in your PC ? make all religion settings to be arabic.
    But in your case, end users must install Arabic languges in there PCs. which is not a good practices

  • Hi
    Thanks for your replay...
    Now i am able to type arabic digits in textbox. But only after typing an arabic alphabet. Therefore i have to extract digits from the textbox. If you can remember something from Old vb6, plz let me know... Thanks...

  • Care for a LINQ method?

    public static string GetNumberArabic(int index)
    {
    CultureInfo ci = new CultureInfo("ar-AE");

    int number = index;

    StringBuilder res = new StringBuilder((int)Math.Log10(index));
    do
    {
    res.Append(ci.NumberFormat.NativeDigits[number % 10]);
    number /= 10;
    } while (number > 0);

    return res.ToString().ToCharArray().Select(ch => ch.ToString()).Aggregate((accumulator, a) => a + accumulator);
    }

  • hello again

    my page is UTF-8

    it uploads an access databse to the server (.mdb)

    it reads the data and displays some records in a gridview , every thing is OK

    but the data in the gridview is not in the apporpriate encoding

  • شكراً عبدالله، وعيد مبارك عليك انشالله

  • That was great but I need the numbers to be displayed in arabic for the paging of a gridview or the numbers of a calendar? How can I do that? I think the function mentioned here can't do that ??

  • Hi guys...

    (Note: Sorry, standing in the garage having a quick smoke, ran across this and had to reply real quick...)

    One VERY easy way to do this that works for ALL languages (i.e. ANY to ANY) is to simply pay attention to the: .NumberFormat.NativeDigits.

    The NativeDigits is an array of 10 characters that represent the numbers for that culture, where '0' is in the 0 index position.

    For example, to convert from English Culture to Arabic (any to any really):

    1. Create an instance of the SOURCE culture:
    CultureInfo sourceCulture = new CultureInfo("En");

    2. Create an instance of the TARGET culture:
    CultureInfo targetCulture = new CultureInfo("ar-AE");

    3. Now, simply iterate through you SOURCE string, find the character in the CURRENT culture array, and take that same character from that index location in the TARGET array:

    // sourceValue is assumed to be in the SOURCEculture for this example

    string sourceValue = "12345";

    // Throw the NativeDigits into a Collection for simplicity
    Collection items = new Collection(sourceCulture.NumberFormatInfo.NativeDigits);

    // Create a string to hold our new value
    // Yea, yea - should be a StringBuilder, this is a sample though :)

    string convertedValue = String.Empty;

    // Now iterate and convert...
    for (int i = 0; i = 0)
    {
    // Now take the corresponding position value
    convertedValue += targetCulture.NumberFormatInfo.NativeDigits[idx];
    }
    else
    {
    // oops, we found some other character
    // bah, just shove it in
    convertedValue += newChar;
    }
    }

    (Sorry, didn't compile this and my smoke is done :) Time to go inside. I think you get the drift...)

  • Hi guys...

    (Note: Sorry, standing in the garage having a quick smoke, ran across this and had to reply real quick...)

    One VERY easy way to do this that works for ALL languages (i.e. ANY to ANY) is to simply pay attention to the: .NumberFormat.NativeDigits.

    The NativeDigits is an array of 10 characters that represent the numbers for that culture, where '0' is in the 0 index position.

    For example, to convert from English Culture to Arabic (any to any really):

    1. Create an instance of the SOURCE culture:
    CultureInfo sourceCulture = new CultureInfo("En");

    2. Create an instance of the TARGET culture:
    CultureInfo targetCulture = new CultureInfo("ar-AE");

    3. Now, simply iterate through you SOURCE string, find the character in the CURRENT culture array, and take that same character from that index location in the TARGET array:

    // sourceValue is assumed to be in the SOURCEculture for this example

    string sourceValue = "12345";

    // Throw the NativeDigits into a Collection for simplicity
    Collection items = new Collection(sourceCulture.NumberFormatInfo.NativeDigits);

    // Create a string to hold our new value
    // Yea, yea - should be a StringBuilder, this is a sample though :)

    string convertedValue = String.Empty;

    // Now iterate and convert...
    for (int i = 0; i = 0)
    {
    // Now take the corresponding position value
    convertedValue += targetCulture.NumberFormatInfo.NativeDigits[idx];
    }
    else
    {
    // oops, we found some other character
    // bah, just shove it in
    convertedValue += newChar;
    }
    }

    (Sorry, didn't compile this and my smoke is done :) Time to go inside. I think you get the drift...)

  • Displaying Arabic Letters just like abdul example i need for letters or words conversion

  • Salam Abdulla AbdelHaq,

    thanks for share a very nice and easy way to Convert Numbers into Arabic Numbers

    May you Help me for same I need in Crystal Reports.
    I need to convert Number into Arabic in Crystal Reports.
    Please tell me same Easy way to find out with Crystal Reports.


    best regards
    Asma Qureshi

  • salam abdulla
    your code helped me a lot

  • Very informative post. Thanks for taking the time to share your view with us.

  • Very informative post. Thanks for taking the time to share your view with us.

  • I’ve been visiting your blog for a while now and I always find a gem in your new posts. Thanks for sharing.

  • Very informative post. Thanks for taking the time to share your view with us.

  • I just sent this post to a bunch of my friends as I agree with most of what you’re saying here and the way you’ve presented it is awesome.

  • I just book marked your blog on Digg and StumbleUpon.I enjoy reading your commentaries.

  • I just sent this post to a bunch of my friends as I agree with most of what you’re saying here and the way you’ve presented it is awesome.

  • I’ve been visiting your blog for a while now and I always find a gem in your new posts. Thanks for sharing.

  • I’ve been visiting your blog for a while now and I always find a gem in your new posts. Thanks for sharing.

  • I find myself coming to your blog more and more often to the point where my visits are almost daily now!

  • I just sent this post to a bunch of my friends as I agree with most of what you’re saying here and the way you’ve presented it is awesome.

  • I just book marked your blog on Digg and StumbleUpon.I enjoy reading your commentaries.

  • I just sent this post to a bunch of my friends as I agree with most of what you’re saying here and the way you’ve presented it is awesome.

  • I find myself coming to your blog more and more often to the point where my visits are almost daily now!

  • You certainly deserve a round of applause for your post and more specifically, your blog in general. Very high quality material

  • I find myself coming to your blog more and more often to the point where my visits are almost daily now!

  • I’ve been visiting your blog for a while now and I always find a gem in your new posts. Thanks for sharing.

  • Very informative post. Thanks for taking the time to share your view with us.

  • I just book marked your blog on Digg and StumbleUpon.I enjoy reading your commentaries.

  • Very informative post. Thanks for taking the time to share your view with us.

  • You certainly deserve a round of applause for your post and more specifically, your blog in general. Very high quality material

  • I just book marked your blog on Digg and StumbleUpon.I enjoy reading your commentaries.

  • You certainly deserve a round of applause for your post and more specifically, your blog in general. Very high quality material

  • I just book marked your blog on Digg and StumbleUpon.I enjoy reading your commentaries.

  • You certainly have some agreeable opinions and views. Your blog provides a fresh look at the subject.

  • Two wrongs don't make a right

  • I’ve been visiting your blog for a while now and I always find a gem in your new posts. Thanks for sharing.

  • Please let me know how can I convert Numericals (in en) into other cultures (Languages ex: fr-FR,zh-TW).

  • I just book marked your blog on Digg and StumbleUpon.I enjoy reading your commentaries.

  • You certainly deserve a round of applause for your post and more specifically, your blog in general. Very high quality material

  • Wow.. cool. This is useful for international communication.

    The concept can actually be modified to do lots of useful features for websites.

  • I just sent this post to a bunch of my friends as I agree with most of what you’re saying here and the way you’ve presented it is awesome.

  • Hi,

    Thanks for sharing a very nice way to Convert Numbers into Arabic Numbers.
    Do you know how to convert table data in english numbers to arabic numbers in Oracle reports.

  • I’ve been visiting your blog for a while now and I always find a gem in your new posts. Thanks for sharing.

  • Hello Abdullah,
    thank you for this solution, it was nice doing. I needed C# version of this code and this example from a guy in this page helped med.
    But right now I need conversation from Arabic to English digits. Can you consider a solution for it in C# please, thanks in advance

    // this is C# version
    public string ConvertToArabicNumerals(string input)
    {

    UTF8Encoding utf8Encoder = new UTF8Encoding();
    Decoder utf8Decoder = utf8Encoder.GetDecoder();
    StringBuilder convertedChars = new System.Text.StringBuilder();

    char[] convertedChar = new char[1];
    byte[] bytes = new byte[] { 217, 160 };
    char[] inputCharArray = input.ToCharArray();

    foreach (char c in inputCharArray)
    {
    if (char.IsDigit(c))
    {
    bytes[1] = Convert.ToByte(160 + char.GetNumericValue(c));
    utf8Decoder.GetChars(bytes, 0, 2, convertedChar, 0);
    convertedChars.Append(convertedChar[0]);
    }
    else
    {
    convertedChars.Append(c);
    }
    }

    return convertedChars.ToString();
    }

  • hi. thanks
    for your converting numbers in c# asp.net!
    that was for your page Rank!!!
    from IRAN

  • hello abdullah
    I hava a question about the numbers 217,160 that you used them here "Dim bytes() As Byte = {217, 160}"
    in your converter code .what does realy they mean.
    I undrestand that "utf8Decoder.GetChars" decodes a sequence of encoded bytes in squnce of characters
    but why should be the first byte 217 and seconod byte 160 + char.to_int.
    thanks.





  • An individual built a number of advantageous facts presently there. I did looking with regard to inside the challenge and also noticed a great deal of men and women might agree inside your internet page.

  • Great, I saw your website from yahoo and the post really interested me ~

  • You certainly deserve a round of applause for your post and more specifically, your blog in general. Very high quality material

  • I find myself coming to your blog more and more often to the point where my visits are almost daily now!

  • Wonderful post - I was looking for a similar article. Thanks for sharing this article to your reader. You give very nice information about this article

  • Good Article.Thanks for sharing.

  • Thanks it Really Helps.

    Very Good Article.

  • Great posting. keep it going. Thanks for sharing.

  • There one huge mistake, a cultural one, the arabic numbers are : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, and the others are hindu numbers, any way great post on how handling the hindu numbers ;)

  • TbUw7J This is one awesome article.Thanks Again.

  • thanks this really help me a lot

  • The post is awesome.. Mr. abdullah,
    but how to change the database values into arabic as u said maintain one more column for arab, so how the arab chars will b stored in database can u pls clarify , pls provide me a sample (To convert database values into arab)

    Thanks in Advance.

  • hi abdullah! thanks for year great topic, thanks for displaying Quran!

  • 4FEpbX A round of applause for your article.Really looking forward to read more. Will read on...

  • aCqh3i Really informative blog article.Thanks Again. Will read on...

  • salam abdullah hope you still there
    my issue is that am displaying arabic data in my gridview the issue is that the characters are encoded badly
    forgive my spelling mistakes but am getting mad with this
    thanks in advance

  • jMcVRz Major thanks for the blog article. Really Cool.

  • 8K1GBB Im grateful for the post.Much thanks again. Will read on...

  • I feel this is one of the such a lot vital info for me.
    And i am satisfied studying your article. However want to observation on some basic things, The
    web site taste is great, the articles is in reality great : D.

    Just right process, cheers

  • Great post.Much thanks again. Awesome.

  • It's going to be end of mine day, but before finish I am reading this fantastic article to improve my experience.

  • It's really a cool and helpful piece of info. I am happy that you simply shared this useful info with us. Please stay us informed like this. Thanks for sharing.

  • It's in fact very difficult in this busy life to listen news on TV, therefore I only use web for that reason, and obtain the newest information.

  • 9eoSU4 This is one awesome post.Much thanks again. Fantastic.

  • When someone writes an article he/she retains the idea of a user in his/her brain that how a user
    can be aware of it. Thus that's why this article is perfect. Thanks!

  • Hello, its nice article about media print, we all be familiar with media is a great source
    of information.

  • Hi there just wanted to give you a quick heads up. The text in your article seem to be running off the screen in Ie.
    I'm not sure if this is a format issue or something to do with web browser compatibility but I thought I'd post to let you know.
    The design and style look great though! Hope you get the issue solved soon.
    Many thanks

  • السلم عليكم ورحمة الله وبركاته
    how to Displaying Arabic Number in GridView

  • Good day! This post could not be written any better!
    Reading through this post reminds me of my old room mate!

    He always kept chatting about this. I will forward this write-up to
    him. Fairly certain he will have a good read.
    Many thanks for sharing!

  • Hi there Dear, are you in fact visiting this website regularly, if so afterward you will absolutely
    take good knowledge.

  • I'm really enjoying the theme/design of your weblog. Do you ever run into any internet browser compatibility issues? A few of my blog readers have complained about my blog not working correctly in Explorer but looks great in Chrome. Do you have any recommendations to help fix this issue?

  • certainly like your web site but you have to take a look at
    the spelling on several of your posts. Many of them are rife with spelling
    issues and I in finding it very bothersome to inform the
    reality however I will surely come back again.

  • I am regular visitor, how are you everybody? This piece of writing posted at this
    web page is genuinely nice.

  • I am sure this post has touched all the internet visitors, its really really nice post on building up new webpage.

Comments have been disabled for this content.