Share via


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.

Example

Imports System
Imports Microsoft.VisualBasic

Public Class TestClass
   
   Public Shared Sub Main()
      ' Parses a string of ASCII digits 1-5.
      Dim strDigits As [String] = "12345"
      ' Parsing succeeds.
      Parse(strDigits)
      
      ' Parses a string ASCII Digits 1-5 specified as Unicode code values.
      Dim strUdigits As [String] = "\u0031\u0032\u0033\u0034\u0035"
      ' Parsing succeeds.
      Parse(strUdigits)
      
      ' Parses a string of Fullwidth digits 1-5 
      ' specified as Unicode code values. 
      Dim strFdigits As [String] = "\uFF11\uFF12\uFF13\uFF14\uFF15"
      ' Parsing fails.
      Parse(strFdigits)
      
      ' Parses a string of Arabic-Indic digits 1-5 
      ' specified as Unicode code values.
      Dim strAdigits As [String] = "\u0661\u0662\u0663\u0664\u0665"
      ' Parsing fails.
      Parse(strAdigits)
      
      ' Parses a string of Bengali digits 1-5 
      ' specified as Unicode code values.
      Dim strBdigits As [String] = "\u09E7\u09E8\u09E9\u09EA\u09EB"
      ' Parsing fails.
      Parse(strBdigits)
   End Sub   
   
   Public Shared Sub Parse(str As [String])
      Try
         Dim dc As [Decimal] = [Decimal].Parse(str)
         Console.WriteLine("Parsing string {0} succeeded: {1} " + _
            ControlChars.Newline, str, dc)
      Catch e As Exception
         Console.WriteLine("Parsing string {0} failed", str)
         Console.WriteLine(e.ToString())
         Console.WriteLine(ControlChars.Newline)
      End Try
   End Sub
End Class

See Also

Concepts

Unicode in the .NET Framework