当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C# Convert.ToInt32(String, IFormatProvider)用法及代码示例


此方法用于使用指定的区域性特定格式信息将数字的指定字符串表示形式转换为等效的32位带符号整数。

用法:

public static int ToInt32 (string value, IFormatProvider provider);

参数:


  • value:它是一个包含要转换的数字的字符串。
  • provider:提供特定于区域性的格式信息的对象。

返回值:此方法返回一个32位有符号整数,该整数等于value中的数字,如果value为null,则返回0(零)。

异常:

  • FormatException:如果该值不包含可选符号,后跟数字序列(0到9)。
  • OverflowException:如果值表示小于MinValue或大于MaxValue的数字。

以下示例程序旨在说明Convert.ToInt32(String,IFormatProvider)方法的用法:

范例1:

// C# program to demonstrate the 
// Convert.ToInt32() Method 
using System; 
using System.Globalization; 
  
class GFG { 
  
// Main Method 
public static void Main() 
{ 
    try { 
  
        // creating object of CultureInfo 
        CultureInfo cultures = new CultureInfo("en-US"); 
  
        // declaring and initializing String array 
        string[] values = {"12345", "+12345", 
                                   "-12345"}; 
  
        // calling get() Method 
        Console.Write("Converted int value "
                    + "of specified strings:"); 
  
        for (int j = 0; j < values.Length; j++)  
        { 
            get(values[j], cultures); 
        } 
    } 
  
    catch (FormatException e)  
    { 
  
        Console.WriteLine("\n"); 
        Console.Write("Exception Thrown:"); 
        Console.Write("{0}", e.GetType(), e.Message); 
    } 
  
    catch (OverflowException e)  
    { 
  
        Console.WriteLine("\n"); 
        Console.Write("Exception Thrown:"); 
        Console.Write("{0}", e.GetType(), e.Message); 
    } 
} 
  
// Defining get() method 
public static void get(string s, 
           CultureInfo cultures) 
{ 
  
    // converting string to specified int 
    int val = Convert.ToInt32(s, cultures); 
  
    // display the converted char value 
    Console.Write(" {0}, ", val); 
} 
}
输出:
Converted int value of specified strings: 12345,  12345,  -12345,

范例2:对于FormatException

// C# program to demonstrate the 
// Convert.ToInt32() Method 
using System; 
using System.Globalization; 
  
class GFG { 
  
// Main Method 
public static void Main() 
{ 
      
try { 
  
    // creating object of CultureInfo 
    CultureInfo cultures = new CultureInfo("en-US"); 
  
    // declaring and initializing String array 
    string[] values = {"12345", "+12345", 
                              "-12345" }; 
  
    // calling get() Method 
    Console.WriteLine("Converted int value"
              + " of specified strings:"); 
  
    for (int j = 0; j < values.Length; j++) 
    { 
  
        get(values[j], cultures); 
    } 
  
    Console.WriteLine("\n"); 
    string s = "123 456, 789"; 
  
    Console.WriteLine("format of s is invalid "); 
  
    // converting string to specified char 
    int val = Convert.ToInt32(s, cultures); 
  
    // display the converted char value 
    Console.Write(" {0}, ", val); 
} 
  
catch (FormatException e) { 
  
    Console.Write("Exception Thrown:"); 
    Console.Write("{0}", e.GetType(), e.Message); 
} 
catch (OverflowException e) { 
  
    Console.Write("Exception Thrown:"); 
    Console.Write("{0}", e.GetType(), e.Message); 
} 
} 
  
// Defining get() method 
public static void get(string s, 
           CultureInfo cultures) 
{ 
  
// converting string to 
// specified int value 
int val = Convert.ToInt32(s, cultures); 
  
// display the converted int value 
Console.Write(" {0}, ", val); 
} 
}
输出:
Converted int value of specified strings:
 12345,  12345,  -12345, 

format of s is invalid 
Exception Thrown:System.FormatException

范例3:对于OverFlowException

// C# program to demonstrate the 
// Convert.ToInt32() Method 
using System; 
using System.Globalization; 
  
class GFG { 
  
// Main Method 
public static void Main() 
{ 
      
    try { 
  
        // creating object of CultureInfo 
        CultureInfo cultures = new CultureInfo("en-US"); 
  
        // declaring and initializing String array 
        string[] values = {"12345", "+12345", 
                                  "-12345" }; 
  
        // calling get() Method 
        Console.WriteLine("Converted int value "
                    + "of specified strings:"); 
  
        for (int j = 0; j < values.Length; j++)  
        { 
  
            get(values[j], cultures); 
        } 
  
        Console.WriteLine("\n"); 
        string s = "-7922816251426433759354395033500000"; 
  
        Console.WriteLine("s is less than the MinValue"); 
  
        // converting string to specified char 
        int val = Convert.ToInt32(s, cultures); 
  
        // display the converted char value 
        Console.Write(" {0}, ", val); 
    } 
      
    catch (FormatException e)  
    { 
        Console.Write("Exception Thrown:"); 
        Console.Write("{0}", e.GetType(), e.Message); 
    } 
      
    catch (OverflowException e)  
    { 
        Console.Write("Exception Thrown:"); 
        Console.Write("{0}", e.GetType(), e.Message); 
    } 
} 
  
// Defining get() method 
public static void get(string s, 
           CultureInfo cultures) 
{ 
  
    // converting string to 
    // specified int value 
    int val = Convert.ToInt32(s, cultures); 
  
    // display the converted int value 
    Console.Write(" {0}, ", val); 
} 
}
输出:
Converted int value of specified strings:
 12345,  12345,  -12345, 

s is less than the MinValue
Exception Thrown:System.OverflowException

参考:



相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 C# | Convert.ToInt32(String, IFormatProvider) Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。