6

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.

stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
bGuruprasad.com
  • 362
  • 3
  • 10
  • Looks like a duplicate of http://stackoverflow.com/questions/7826162/how-do-i-get-the-decimal-value-of-a-unicode-character-in-c. – David Peden May 19 '13 at 05:44
  • Is there a logic where you can map characters to integers and then calculate from there? Something like [parsing roman numbers](http://vadivel.blogspot.de/2011/09/how-to-convert-roman-numerals-to.html) – Corak May 19 '13 at 05:45
  • 1
    FYI, `int.Parse()` isn't likely to work in this case, since the documentation for [`NumberFormatInfo.NativeDigits`](http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.nativedigits.aspx) says: _"The character set that is specified by the `NativeDigits` property has no effect on parsing or formatting operations. Only the Basic Latin digits `0` (U+0030) through `9` (U+0039) are used when formatting or parsing numeric values or date and time values."_ – stakx - no longer contributing May 19 '13 at 09:25
  • 1
    @Guruprasad - You should accept the answer rather than putting {Solved} in the title. – LeopardSkinPillBoxHat May 20 '13 at 04:39

4 Answers4

4

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"))
stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • 1
    This is the right answer but it's disappointing you need to go so low-level, what with textual support being so good. – Basic May 19 '13 at 09:51
  • 1
    @Alexei Levenkov Thanks for the solution but the code fails when value is in decimal format (like १२३.३२१ = latin Equ. 123.321).. – bGuruprasad.com May 19 '13 at 10:02
2

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

zero323
  • 322,348
  • 103
  • 959
  • 935
Roger Johansson
  • 22,764
  • 18
  • 97
  • 193
0

I found my solution... The following code will convert given Marathi number to its equivalent Latin number..
Thanks to @Alexei, I just changed some of your code and its working fine..

 string ToLatinDigits(string nativeDigits)
    {
        int n = nativeDigits.Length;
        StringBuilder latinDigits = new StringBuilder(capacity: n);
        for (int i = 0; i < n; ++i)
        {
            if (char.IsDigit(nativeDigits, i))
            {
                latinDigits.Append(char.GetNumericValue(nativeDigits, i));
            }
            else if (nativeDigits[i].Equals('.') || nativeDigits[i].Equals('+') || nativeDigits[i].Equals('-'))
            {
                latinDigits.Append(nativeDigits[i]);
            }
            else
            {
                throw new Exception("Invalid Argument");
            }
        }
        return latinDigits.ToString();
    }

This method is working for both + and - numbers.
Regards Guruprasad

bGuruprasad.com
  • 362
  • 3
  • 10
0

Windows.Globalization.DecimalFormatter will parse different numeral systems in addition to Latin, including Devanagari (which is what is used by Marathi).

Eric MSFT
  • 3,246
  • 1
  • 18
  • 28