Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Unicode string made up of culture-specific digits to integer value

I am developing a program in the Marathi language. In it, I want to add/validate numbers entered in Marathi Unicode by getting their actual integer value.

For example, in Marathi:

  • ४५ = 45
  • ९९ = 99

How do I convert this Marathi string "४५" to its actual integer value i.e. 45?

I googled a lot, but found nothing useful. I tried using System.Text.Encoding.Unicode.GetString() to get string and then tried to parse, but failed here also.

like image 785
bGuruprasad.com Avatar asked May 19 '13 05:05

bGuruprasad.com


2 Answers

Correct way would be to use Char.GetNumericValue that lets you to convert individual characters to corresponding numeric values and than construct complete value. I.e. Char.GetNumericValue('९') gives you 9.

Depending on your goal it may be easier to replace each national digit character with corresponding invariant digit and use regular parsing functions.

Int32.Parse("९९".Replace("९", "9"))
like image 84
Alexei Levenkov Avatar answered Nov 05 '22 11:11

Alexei Levenkov


Quick hack of @Alexi's response.

public static double ParseValue(string value)
{
    return double.Parse(string.Join("",
        value.Select(c => "+-.".Contains(c)
           ? "" + c: "" + char.GetNumericValue(c)).ToArray()),
        NumberFormatInfo.InvariantInfo);
}

calling ParseValue("१२३.३२१") yields 123.321 as result

like image 33
Roger Johansson Avatar answered Nov 05 '22 11:11

Roger Johansson