Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 30-Jan-12 23:29pm
Comments
Manfred R. Bihy at 31-Jan-12 6:04am
   
Very good question!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

According to the MSDN .NET 4 doucmentation regarding the Parsing Of Numeric Strings[^] you are, sad but true , on your own.
 
Regards,
 
Manfred
  Permalink  
Comments
SAKryukov at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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" (Smile | :) ) 01234567890 before parsing albeit not in that naive way mentioned by MohammadAmiry (Smile | :) ), 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
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

I had to do the following:
Long.Parse(sDigits.Replace("٠", "0").Replace("١", "1").Replace("٢", "2").Replace("٣", "3").Replace("٤", "4").Replace("٥", "5").Replace("٦", "6").Replace("٧", "7").Replace("٨", "8").Replace("٩", "9"))
  Permalink  
Comments
SAKryukov at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 686
1 OriginalGriff 498
2 Tadit Dash 420
3 sanket saxena 337
4 Zoltán Zörgő 230
0 Sergey Alexandrovich Kryukov 11,800
1 OriginalGriff 7,225
2 Peter Leow 5,009
3 Abhinav S 3,893
4 Maciej Los 3,575


Advertise | Privacy | Mobile
Web03 | 2.8.140421.2 | Last Updated 31 Jan 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid