Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,
I would like to use .net Long.Parse("١٢٣٤٥٦٧٨٩٠") to get 1234567890 but it gives an error and does not recognizes these Indic digits as numbers.
How can I do that?

Thanks,
Mohammad Amiry
Posted
Comments
Manfred Rudolf Bihy 31-Jan-12 6:04am    
Very good question!

If you can translate the string into Latin numerals, you can then parse that string. I've never used internationalization and Unicode, but I would think you could step through the string one character at a time and use a case statement to compare and translate. Rebuild the string using a StringBuilder (which is more efficient than concatinating ordinary strings) and pass the result into Parse. A nuisance, certainly.

Once you have an algorithm that works, you could extend the Int32[^] object to add a ParseFromIndic function, which will make your code writing quite a bit easier.

Revised answer - Did some research and found that extension methods only work on instanced variables; as of .Net 4.0, you cannot write static extensions. However, you could write a String extension with a name like ParseIndicNumber which attempts to convert the value of the string instance into a number.
 
Share this answer
 
v2
According to the MSDN .NET 4 doucmentation regarding the Parsing Of Numeric Strings[^] you are, sad but true , on your own.

Regards,

Manfred
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 31-Jan-12 19:54pm    
Good point and reference but sounds way too pessimistic. There is no reason for sadness; please see my answer. (I voted 4 this time; sad but... :-))
--SA
This is actually called Indo-Arabic numerals, more exactly called "Eastern Arabic", funny that Arabs call then "Hindi numerals", and Europeans (and many other) call 0123456789 "Arabic digits"; see http://en.wikipedia.org/wiki/Arabic_digits[^].

It looks like they are not parsed properly, so I'm afraid this is the only way: they should be replaced with "Arabic digits" (:-)) 01234567890 before parsing albeit not in that naive way mentioned by MohammadAmiry (:-)), but by subtracting of a constant shift from a Unicode code point (please calculate it by yourself, this is too easy; the shift is possible because the order of digits is exactly the same in both systems).

—SA
 
Share this answer
 
v2
I had to do the following:
C#
Long.Parse(sDigits.Replace("٠", "0").Replace("١", "1").Replace("٢", "2").Replace("٣", "3").Replace("٤", "4").Replace("٥", "5").Replace("٦", "6").Replace("٧", "7").Replace("٨", "8").Replace("٩", "9"))
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 31-Jan-12 19:52pm    
Very naive, but the idea is basically right, so it does not deserve a vote of 1.
Please see my answer.
--SA

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900