Export (0) Print
Expand All

How to: Parse Unicode Digits

The Unicode Standard defines code values for digits in various scripts. For example, code values in the range U+09E6 through U+09EF specify the Bengali digits 0 through 9, and code values in the range U+FF10 through U+FF19 specify the Fullwidth digits 0 through 9. The only Unicode digits that the .NET Framework parses as decimals are the ASCII digits 0 through 9, specified by the code values U+0030 through U+0039. The .NET Framework parses all other Unicode digits as characters. Therefore, when an application attempts to parse a string of Bengali digits in the range U+09E6 through U+09EF using the Decimal.Parse method, an exception is thrown. For more information, see The Unicode Standard at the Unicode home page.

The following example uses the Parse method to parse strings of Unicode code values that specify digits in different scripts. The attempts to parse ASCII digits and ASCII digits specified as Unicode code values succeed. The attempts to parse the Unicode code values for Fullwidth digits, Arabic-Indic digits, and Bengali digits fail and throw an exception.

using System;

public class TestClass
{
   public static void Main() 
   {
      // Parses a string of ASCII digits 1-5.
      String strDigits = "12345";
      // Parsing succeeds.
      Parse(strDigits);
      
      // Parses a string of ASCII Digits 1-5 specified 
      // as Unicode code values.
      String strUdigits = "\u0031\u0032\u0033\u0034\u0035";
      // Parsing succeeds.
      Parse(strUdigits);
      
      // Parses a string of Fullwidth digits 1-5 specified as
      // Unicode code values. 
      String strFdigits = "\uFF11\uFF12\uFF13\uFF14\uFF15";
      // Parsing fails.
      Parse(strFdigits);
      
      // Parses a string of Arabic-Indic digits 1-5 specified as
      // Unicode code values.
      String strAdigits = "\u0661\u0662\u0663\u0664\u0665";
      // Parsing fails.
      Parse(strAdigits);
      
      // Parses a string of Bengali digits 1-5 specified as
      // Unicode code values.
      String strBdigits = "\u09E7\u09E8\u09E9\u09EA\u09EB";
      // Parsing fails.
      Parse(strBdigits);
   }

   public static void Parse(String str)
   {
      try 
      {
         Decimal dc = Decimal.Parse(str);
         Console.WriteLine("Parsing string {0} succeeded: {1}\n",str, dc);
      }
      catch (Exception e) 
      {
         Console.WriteLine("Parsing string {0} failed", str);
         Console.WriteLine(e.ToString());
         Console.WriteLine("\n");
      }
   }
}

Community Additions

Show:
© 2014 Microsoft