简体   繁体   中英

How to convert numeric value in English to Marathi numeric value in C#?

I am developing an windows application in C#, for that I need to convert English numeric value to Marathi numeric value. For Example. "123" = "१२३"

The most obviose way is use String.Replace method and write helper class.

public class MarathiHelper
{
    private static Dictionary<char, char> arabicToMarathi = new Dictionary<char, char>()
    {
      {'1','१'},
      {'2','२'},
      {'3','३'},
      {'4','४'},
      {'5','५'},
      {'6','६'},
      {'7','७'},
      {'8','८'},
      {'9','९'},
      {'0','०'},
    };

    public static string ReplaceNumbers(string input)
    {
        foreach (var num in arabicToMarathi)
        {
            input = input.Replace(num.Key, num.Value);
        }
        return input;
    }

}

And in your code you can use it like this:

var marathi = MarathiHelper.ReplaceNumbers("123");

marathi will have "१२३"

Well, in order to convert every character in ['0'..'9'] should be shifted by 0x0966 - '0' ; and the implementation could be

  string source = "The number is 0123456789";

  string result = new String(source
    .Select(c => c >= '0' && c <= '9' ? (Char) (c - '0' + 0x0966) : c)
    .ToArray()); 

The outcome ( result ) is

  The number is ०१२३४५६७८९

Note, that Char.IsDigit(c) is not an option here, since we don't want to shift Marathi numbers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM