當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。