Int32 結構

定義

表示 32 位元帶正負號的整數。

public value class int : IComparable, IComparable<int>, IConvertible, IEquatable<int>, IFormattable
public value class int : IComparable, IComparable<int>, IConvertible, IEquatable<int>, ISpanFormattable
public value class int : IComparable<int>, IConvertible, IEquatable<int>, IParsable<int>, ISpanParsable<int>, System::Numerics::IAdditionOperators<int, int, int>, System::Numerics::IAdditiveIdentity<int, int>, System::Numerics::IBinaryInteger<int>, System::Numerics::IBinaryNumber<int>, System::Numerics::IBitwiseOperators<int, int, int>, System::Numerics::IComparisonOperators<int, int, bool>, System::Numerics::IDecrementOperators<int>, System::Numerics::IDivisionOperators<int, int, int>, System::Numerics::IEqualityOperators<int, int, bool>, System::Numerics::IIncrementOperators<int>, System::Numerics::IMinMaxValue<int>, System::Numerics::IModulusOperators<int, int, int>, System::Numerics::IMultiplicativeIdentity<int, int>, System::Numerics::IMultiplyOperators<int, int, int>, System::Numerics::INumber<int>, System::Numerics::INumberBase<int>, System::Numerics::IShiftOperators<int, int, int>, System::Numerics::ISignedNumber<int>, System::Numerics::ISubtractionOperators<int, int, int>, System::Numerics::IUnaryNegationOperators<int, int>, System::Numerics::IUnaryPlusOperators<int, int>
public value class int : IComparable<int>, IConvertible, IEquatable<int>, IParsable<int>, ISpanParsable<int>, IUtf8SpanFormattable, IUtf8SpanParsable<int>, System::Numerics::IAdditionOperators<int, int, int>, System::Numerics::IAdditiveIdentity<int, int>, System::Numerics::IBinaryInteger<int>, System::Numerics::IBinaryNumber<int>, System::Numerics::IBitwiseOperators<int, int, int>, System::Numerics::IComparisonOperators<int, int, bool>, System::Numerics::IDecrementOperators<int>, System::Numerics::IDivisionOperators<int, int, int>, System::Numerics::IEqualityOperators<int, int, bool>, System::Numerics::IIncrementOperators<int>, System::Numerics::IMinMaxValue<int>, System::Numerics::IModulusOperators<int, int, int>, System::Numerics::IMultiplicativeIdentity<int, int>, System::Numerics::IMultiplyOperators<int, int, int>, System::Numerics::INumber<int>, System::Numerics::INumberBase<int>, System::Numerics::IShiftOperators<int, int, int>, System::Numerics::ISignedNumber<int>, System::Numerics::ISubtractionOperators<int, int, int>, System::Numerics::IUnaryNegationOperators<int, int>, System::Numerics::IUnaryPlusOperators<int, int>
public value class int : IComparable, IConvertible, IFormattable
public value class int : IComparable, IComparable<int>, IEquatable<int>, IFormattable
public struct Int32 : IComparable, IComparable<int>, IConvertible, IEquatable<int>, IFormattable
public readonly struct Int32 : IComparable, IComparable<int>, IConvertible, IEquatable<int>, IFormattable
public readonly struct Int32 : IComparable, IComparable<int>, IConvertible, IEquatable<int>, ISpanFormattable
public readonly struct Int32 : IComparable<int>, IConvertible, IEquatable<int>, IParsable<int>, ISpanParsable<int>, System.Numerics.IAdditionOperators<int,int,int>, System.Numerics.IAdditiveIdentity<int,int>, System.Numerics.IBinaryInteger<int>, System.Numerics.IBinaryNumber<int>, System.Numerics.IBitwiseOperators<int,int,int>, System.Numerics.IComparisonOperators<int,int,bool>, System.Numerics.IDecrementOperators<int>, System.Numerics.IDivisionOperators<int,int,int>, System.Numerics.IEqualityOperators<int,int,bool>, System.Numerics.IIncrementOperators<int>, System.Numerics.IMinMaxValue<int>, System.Numerics.IModulusOperators<int,int,int>, System.Numerics.IMultiplicativeIdentity<int,int>, System.Numerics.IMultiplyOperators<int,int,int>, System.Numerics.INumber<int>, System.Numerics.INumberBase<int>, System.Numerics.IShiftOperators<int,int,int>, System.Numerics.ISignedNumber<int>, System.Numerics.ISubtractionOperators<int,int,int>, System.Numerics.IUnaryNegationOperators<int,int>, System.Numerics.IUnaryPlusOperators<int,int>
public readonly struct Int32 : IComparable<int>, IConvertible, IEquatable<int>, IParsable<int>, ISpanParsable<int>, IUtf8SpanFormattable, IUtf8SpanParsable<int>, System.Numerics.IAdditionOperators<int,int,int>, System.Numerics.IAdditiveIdentity<int,int>, System.Numerics.IBinaryInteger<int>, System.Numerics.IBinaryNumber<int>, System.Numerics.IBitwiseOperators<int,int,int>, System.Numerics.IComparisonOperators<int,int,bool>, System.Numerics.IDecrementOperators<int>, System.Numerics.IDivisionOperators<int,int,int>, System.Numerics.IEqualityOperators<int,int,bool>, System.Numerics.IIncrementOperators<int>, System.Numerics.IMinMaxValue<int>, System.Numerics.IModulusOperators<int,int,int>, System.Numerics.IMultiplicativeIdentity<int,int>, System.Numerics.IMultiplyOperators<int,int,int>, System.Numerics.INumber<int>, System.Numerics.INumberBase<int>, System.Numerics.IShiftOperators<int,int,int>, System.Numerics.ISignedNumber<int>, System.Numerics.ISubtractionOperators<int,int,int>, System.Numerics.IUnaryNegationOperators<int,int>, System.Numerics.IUnaryPlusOperators<int,int>
[System.Serializable]
public struct Int32 : IComparable, IConvertible, IFormattable
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public struct Int32 : IComparable, IComparable<int>, IConvertible, IEquatable<int>, IFormattable
public struct Int32 : IComparable, IComparable<int>, IEquatable<int>, IFormattable
type int = struct
    interface IConvertible
    interface IFormattable
type int = struct
    interface IConvertible
    interface ISpanFormattable
    interface IFormattable
type int = struct
    interface IConvertible
    interface IFormattable
    interface IParsable<int>
    interface ISpanFormattable
    interface ISpanParsable<int>
    interface IAdditionOperators<int, int, int>
    interface IAdditiveIdentity<int, int>
    interface IBinaryInteger<int>
    interface IBinaryNumber<int>
    interface IBitwiseOperators<int, int, int>
    interface IComparisonOperators<int, int, bool>
    interface IEqualityOperators<int, int, bool>
    interface IDecrementOperators<int>
    interface IDivisionOperators<int, int, int>
    interface IIncrementOperators<int>
    interface IModulusOperators<int, int, int>
    interface IMultiplicativeIdentity<int, int>
    interface IMultiplyOperators<int, int, int>
    interface INumber<int>
    interface INumberBase<int>
    interface ISubtractionOperators<int, int, int>
    interface IUnaryNegationOperators<int, int>
    interface IUnaryPlusOperators<int, int>
    interface IShiftOperators<int, int, int>
    interface IMinMaxValue<int>
    interface ISignedNumber<int>
type int = struct
    interface IConvertible
    interface IFormattable
    interface IParsable<int>
    interface ISpanFormattable
    interface ISpanParsable<int>
    interface IAdditionOperators<int, int, int>
    interface IAdditiveIdentity<int, int>
    interface IBinaryInteger<int>
    interface IBinaryNumber<int>
    interface IBitwiseOperators<int, int, int>
    interface IComparisonOperators<int, int, bool>
    interface IEqualityOperators<int, int, bool>
    interface IDecrementOperators<int>
    interface IDivisionOperators<int, int, int>
    interface IIncrementOperators<int>
    interface IModulusOperators<int, int, int>
    interface IMultiplicativeIdentity<int, int>
    interface IMultiplyOperators<int, int, int>
    interface INumber<int>
    interface INumberBase<int>
    interface ISubtractionOperators<int, int, int>
    interface IUnaryNegationOperators<int, int>
    interface IUnaryPlusOperators<int, int>
    interface IUtf8SpanParsable<int>
    interface IShiftOperators<int, int, int>
    interface IMinMaxValue<int>
    interface ISignedNumber<int>
    interface IUtf8SpanFormattable
[<System.Serializable>]
type int = struct
    interface IFormattable
    interface IConvertible
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type int = struct
    interface IFormattable
    interface IConvertible
type int = struct
    interface IFormattable
Public Structure Int32
Implements IComparable, IComparable(Of Integer), IConvertible, IEquatable(Of Integer), IFormattable
Public Structure Int32
Implements IComparable, IComparable(Of Integer), IConvertible, IEquatable(Of Integer), ISpanFormattable
Public Structure Int32
Implements IAdditionOperators(Of Integer, Integer, Integer), IAdditiveIdentity(Of Integer, Integer), IBinaryInteger(Of Integer), IBinaryNumber(Of Integer), IBitwiseOperators(Of Integer, Integer, Integer), IComparable(Of Integer), IComparisonOperators(Of Integer, Integer, Boolean), IConvertible, IDecrementOperators(Of Integer), IDivisionOperators(Of Integer, Integer, Integer), IEqualityOperators(Of Integer, Integer, Boolean), IEquatable(Of Integer), IIncrementOperators(Of Integer), IMinMaxValue(Of Integer), IModulusOperators(Of Integer, Integer, Integer), IMultiplicativeIdentity(Of Integer, Integer), IMultiplyOperators(Of Integer, Integer, Integer), INumber(Of Integer), INumberBase(Of Integer), IParsable(Of Integer), IShiftOperators(Of Integer, Integer, Integer), ISignedNumber(Of Integer), ISpanParsable(Of Integer), ISubtractionOperators(Of Integer, Integer, Integer), IUnaryNegationOperators(Of Integer, Integer), IUnaryPlusOperators(Of Integer, Integer)
Public Structure Int32
Implements IAdditionOperators(Of Integer, Integer, Integer), IAdditiveIdentity(Of Integer, Integer), IBinaryInteger(Of Integer), IBinaryNumber(Of Integer), IBitwiseOperators(Of Integer, Integer, Integer), IComparable(Of Integer), IComparisonOperators(Of Integer, Integer, Boolean), IConvertible, IDecrementOperators(Of Integer), IDivisionOperators(Of Integer, Integer, Integer), IEqualityOperators(Of Integer, Integer, Boolean), IEquatable(Of Integer), IIncrementOperators(Of Integer), IMinMaxValue(Of Integer), IModulusOperators(Of Integer, Integer, Integer), IMultiplicativeIdentity(Of Integer, Integer), IMultiplyOperators(Of Integer, Integer, Integer), INumber(Of Integer), INumberBase(Of Integer), IParsable(Of Integer), IShiftOperators(Of Integer, Integer, Integer), ISignedNumber(Of Integer), ISpanParsable(Of Integer), ISubtractionOperators(Of Integer, Integer, Integer), IUnaryNegationOperators(Of Integer, Integer), IUnaryPlusOperators(Of Integer, Integer), IUtf8SpanFormattable, IUtf8SpanParsable(Of Integer)
Public Structure Int32
Implements IComparable, IConvertible, IFormattable
Public Structure Int32
Implements IComparable, IComparable(Of Integer), IEquatable(Of Integer), IFormattable
繼承
屬性
實作
IComparable IComparable<Int32> IConvertible IEquatable<Int32> IFormattable ISpanFormattable IComparable<TSelf> IEquatable<TSelf> IParsable<Int32> IParsable<TSelf> ISpanParsable<Int32> ISpanParsable<TSelf> IAdditionOperators<Int32,Int32,Int32> IAdditionOperators<TSelf,TSelf,TSelf> IAdditiveIdentity<Int32,Int32> IAdditiveIdentity<TSelf,TSelf> IBinaryInteger<Int32> IBinaryNumber<Int32> IBinaryNumber<TSelf> IBitwiseOperators<Int32,Int32,Int32> IBitwiseOperators<TSelf,TSelf,TSelf> IComparisonOperators<Int32,Int32,Boolean> IComparisonOperators<TSelf,TSelf,Boolean> IDecrementOperators<Int32> IDecrementOperators<TSelf> IDivisionOperators<Int32,Int32,Int32> IDivisionOperators<TSelf,TSelf,TSelf> IEqualityOperators<Int32,Int32,Boolean> IEqualityOperators<TSelf,TOther,TResult> IEqualityOperators<TSelf,TSelf,Boolean> IIncrementOperators<Int32> IIncrementOperators<TSelf> IMinMaxValue<Int32> IModulusOperators<Int32,Int32,Int32> IModulusOperators<TSelf,TSelf,TSelf> IMultiplicativeIdentity<Int32,Int32> IMultiplicativeIdentity<TSelf,TSelf> IMultiplyOperators<Int32,Int32,Int32> IMultiplyOperators<TSelf,TSelf,TSelf> INumber<Int32> INumber<TSelf> INumberBase<Int32> INumberBase<TSelf> IShiftOperators<Int32,Int32,Int32> IShiftOperators<TSelf,Int32,TSelf> ISignedNumber<Int32> ISubtractionOperators<Int32,Int32,Int32> ISubtractionOperators<TSelf,TSelf,TSelf> IUnaryNegationOperators<Int32,Int32> IUnaryNegationOperators<TSelf,TSelf> IUnaryPlusOperators<Int32,Int32> IUnaryPlusOperators<TSelf,TSelf> IUtf8SpanFormattable IUtf8SpanParsable<Int32> IUtf8SpanParsable<TSelf>

備註

Int32 是不可變的實值型別,代表帶正負號的整數,其值範圍從負數 2,147,483,648 (Int32.MinValue 表示,由常數) 到正數 2,147,483,647 (Int32.MaxValue 表示。 .NET 也包含不帶正負號的 32 位整數實數值型別, UInt32 代表範圍從 0 到 4,294,967,295 的值。

具現化 Int32 值

您可以透過數種方式具現化 Int32 值:

  • 您可以宣告 Int32 變數,並為其指派資料類型範圍內 Int32 常值整數值。 下列範例會宣告兩 Int32 個變數,並以這種方式指派這些變數。

    int number1 = 64301;
    int number2 = 25548612;
    
    let number1 = 64301
    let number2 = 25548612
    
    Dim number1 As Integer = 64301
    Dim number2 As Integer = 25548612
    
  • 您可以指派整數型別的值,其範圍是類型的子集 Int32 。 這是擴輾轉換,不需要 C# 中的轉換運算子或 Visual Basic 中的轉換方法,但需要 F# 中的轉換運算子。

    sbyte value1 = 124;
    short value2 = 1618;
    
    int number1 = value1;
    int number2 = value2;
    
    let value1 = 124y
    let value2 = 1618s
    
    let number1 = int value1
    let number2 = int value2
    
    Dim value1 As SByte = 124
    Dim value2 As Int16 = 1618
    
    Dim number1 As Integer = value1
    Dim number2 As Integer = value2
    
  • 您可以指派數數值型別的值,其範圍超過 Int32 該類型的值。 這是縮小轉換,因此在 C# 或 F# 中需要轉換運算子,如果 開啟,則需要 Visual Basic Option Strict 中的轉換方法。 如果數值是包含 Single 小陣列件的 、 DoubleDecimal 值,則其小數部分的處理取決於執行轉換的編譯器。 下列範例會執行縮小轉換,將數個數值指派給 Int32 變數。

    long lNumber = 163245617;
    try {
       int number1 = (int) lNumber;
       Console.WriteLine(number1);
    }
    catch (OverflowException) {
       Console.WriteLine("{0} is out of range of an Int32.", lNumber);
    }
    
    double dbl2 = 35901.997;
    try {
       int number2 = (int) dbl2;
       Console.WriteLine(number2);
    }
    catch (OverflowException) {
       Console.WriteLine("{0} is out of range of an Int32.", dbl2);
    }
    
    BigInteger bigNumber = 132451;
    try {
       int number3 = (int) bigNumber;
       Console.WriteLine(number3);
    }
    catch (OverflowException) {
       Console.WriteLine("{0} is out of range of an Int32.", bigNumber);
    }
    // The example displays the following output:
    //       163245617
    //       35902
    //       132451
    
    let lNumber = 163245617L
    try
        let number1 = int lNumber
        printfn $"{number1}"
    with :? OverflowException ->
        printfn "{lNumber} is out of range of an Int32."
    
    let dbl2 = 35901.997
    try
        let number2 = int dbl2
        printfn $"{number2}"
    with :? OverflowException ->
        printfn $"{dbl2} is out of range of an Int32."
    
    let bigNumber = BigInteger 132451
    try
        let number3 = int bigNumber
        printfn $"{number3}"
    with :? OverflowException ->
        printfn $"{bigNumber} is out of range of an Int32."
    
    // The example displays the following output:
    //       163245617
    //       35902
    //       132451
    
    Dim lNumber As Long = 163245617
    Try
       Dim number1 As Integer = CInt(lNumber)
       Console.WriteLine(number1)
    Catch e As OverflowException
       Console.WriteLine("{0} is out of range of an Int32.", lNumber)
    End Try
    
    Dim dbl2 As Double = 35901.997
    Try
       Dim number2 As Integer = CInt(dbl2)
       Console.WriteLine(number2)
    Catch e As OverflowException
       Console.WriteLine("{0} is out of range of an Int32.", dbl2)
    End Try
       
    Dim bigNumber As BigInteger = 132451
    Try
       Dim number3 As Integer = CInt(bigNumber)
       Console.WriteLine(number3)
    Catch e As OverflowException
       Console.WriteLine("{0} is out of range of an Int32.", bigNumber)
    End Try    
    ' The example displays the following output:
    '       163245617
    '       35902
    '       132451
    
  • 您可以呼叫 類別的方法, Convert 將任何支援的型別 Int32 轉換為值。 這是可行的,因為 Int32 支援 IConvertible 介面。 下列範例說明將值 Int32 陣列 Decimal 轉換成值。

    decimal[] values= { Decimal.MinValue, -1034.23m, -12m, 0m, 147m,
                        199.55m, 9214.16m, Decimal.MaxValue };
    int result;
    
    foreach (decimal value in values)
    {
       try {
          result = Convert.ToInt32(value);
          Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.",
                            value.GetType().Name, value,
                            result.GetType().Name, result);
       }
       catch (OverflowException) {
          Console.WriteLine("{0} is outside the range of the Int32 type.",
                            value);
       }
    }
    // The example displays the following output:
    //    -79228162514264337593543950335 is outside the range of the Int32 type.
    //    Converted the Decimal value '-1034.23' to the Int32 value -1034.
    //    Converted the Decimal value '-12' to the Int32 value -12.
    //    Converted the Decimal value '0' to the Int32 value 0.
    //    Converted the Decimal value '147' to the Int32 value 147.
    //    Converted the Decimal value '199.55' to the Int32 value 200.
    //    Converted the Decimal value '9214.16' to the Int32 value 9214.
    //    79228162514264337593543950335 is outside the range of the Int32 type.
    
    let values = 
        [| Decimal.MinValue; -1034.23M; -12m; 0M; 147M
           199.55M; 9214.16M; Decimal.MaxValue |]
    
    for value in values do
        try
            let result = Convert.ToInt32 value
            printfn $"Converted the {value.GetType().Name} value '{value}' to the {result.GetType().Name} value {result}."
            
        with :? OverflowException ->
            printfn $"{value} is outside the range of the Int32 type."
       
    // The example displays the following output:
    //    -79228162514264337593543950335 is outside the range of the Int32 type.
    //    Converted the Decimal value '-1034.23' to the Int32 value -1034.
    //    Converted the Decimal value '-12' to the Int32 value -12.
    //    Converted the Decimal value '0' to the Int32 value 0.
    //    Converted the Decimal value '147' to the Int32 value 147.
    //    Converted the Decimal value '199.55' to the Int32 value 200.
    //    Converted the Decimal value '9214.16' to the Int32 value 9214.
    //    79228162514264337593543950335 is outside the range of the Int32 type.
    
    Dim values() As Decimal = { Decimal.MinValue, -1034.23d, -12d, 0d, 147d, _
                                199.55d, 9214.16d, Decimal.MaxValue }
    Dim result As Integer
    
    For Each value As Decimal In values
       Try
          result = Convert.ToInt32(value)
          Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", _
                            value.GetType().Name, value, _
                            result.GetType().Name, result)
       Catch e As OverflowException
          Console.WriteLine("{0} is outside the range of the Int32 type.", _
                            value)
       End Try   
    Next                                  
    ' The example displays the following output:
    '    -79228162514264337593543950335 is outside the range of the Int32 type.
    '    Converted the Decimal value '-1034.23' to the Int32 value -1034.
    '    Converted the Decimal value '-12' to the Int32 value -12.
    '    Converted the Decimal value '0' to the Int32 value 0.
    '    Converted the Decimal value '147' to the Int32 value 147.
    '    Converted the Decimal value '199.55' to the Int32 value 200.
    '    Converted the Decimal value '9214.16' to the Int32 value 9214.
    '    79228162514264337593543950335 is outside the range of the Int32 type.
    
  • 您可以呼叫 ParseTryParse 方法,將值的字串表示 Int32 轉換成 Int32 。 字串可以包含十進位或十六進位數位。 下列範例說明使用十進位和十六進位字串來剖析作業。

    string string1 = "244681";
    try {
       int number1 = Int32.Parse(string1);
       Console.WriteLine(number1);
    }
    catch (OverflowException) {
       Console.WriteLine("'{0}' is out of range of a 32-bit integer.", string1);
    }
    catch (FormatException) {
       Console.WriteLine("The format of '{0}' is invalid.", string1);
    }
    
    string string2 = "F9A3C";
    try {
       int number2 = Int32.Parse(string2,
                                System.Globalization.NumberStyles.HexNumber);
       Console.WriteLine(number2);
    }
    catch (OverflowException) {
       Console.WriteLine("'{0}' is out of range of a 32-bit integer.", string2);
    }
    catch (FormatException) {
       Console.WriteLine("The format of '{0}' is invalid.", string2);
    }
    // The example displays the following output:
    //       244681
    //       1022524
    
    let string1 = "244681"
    try
        let number1 = Int32.Parse string1
        printfn $"{number1}"
    with
    | :? OverflowException ->
        printfn "'{string1}' is out of range of a 32-bit integer."
    | :? FormatException ->
        printfn $"The format of '{string1}' is invalid."
    
    let string2 = "F9A3C"
    try
        let number2 = Int32.Parse(string2, System.Globalization.NumberStyles.HexNumber)
        printfn $"{number2}"
    with 
    | :? OverflowException ->
        printfn $"'{string2}' is out of range of a 32-bit integer."
    | :? FormatException ->
        printfn $"The format of '{string2}' is invalid."
    
    // The example displays the following output:
    //       244681
    //       1022524
    
    Dim string1 As String = "244681"
    Try
       Dim number1 As Integer = Int32.Parse(string1)
       Console.WriteLine(number1)
    Catch e As OverflowException
       Console.WriteLine("'{0}' is out of range of a 32-bit integer.", string1)
    Catch e As FormatException
       Console.WriteLine("The format of '{0}' is invalid.", string1)
    End Try
    
    Dim string2 As String = "F9A3C"
    Try
       Dim number2 As Integer = Int32.Parse(string2,
                                System.Globalization.NumberStyles.HexNumber)
       Console.WriteLine(number2)
    Catch e As OverflowException
       Console.WriteLine("'{0}' is out of range of a 32-bit integer.", string2)
    Catch e As FormatException
       Console.WriteLine("The format of '{0}' is invalid.", string2)
    End Try
    ' The example displays the following output:
    '       244681
    '       1022524
    

對 Int32 值執行作業

Int32 類型支援標準數學運算,例如加法、減法、除法、乘法、負數和一元負數。 與其他整數類型一樣,此 Int32 類型也支援位 ANDORXOR 左移和右移運算子。

您可以使用標準數值運算子來比較兩 Int32 個值,也可以呼叫 CompareToEquals 方法。

您也可以呼叫 類別的成員 Math 來執行廣泛的數值運算,包括取得數位的絕對值、計算整數除法的商數和餘數、判斷兩個整數的最大值或最小值、取得數位的正負號,以及四捨五入數位。

以字串表示 Int32

Int32 類型提供標準和自訂數值格式字串的完整支援。 (如需詳細資訊,請參閱 格式化類型標準數值格式字串自訂數值格式字串s.)

若要將 Int32 值格式化為不含前置零的整數位符串,您可以呼叫無 ToString() 參數方法。 藉由使用 「D」 格式規範,您也可以在字串表示中包含指定數目的前置零。 藉由使用 「N」 格式規範,您可以包含群組分隔符號,並指定要出現在數位字串表示中的十進位數數目。 藉由使用 「X」 格式規範,您可以將值表示 Int32 為十六進位字串。 下列範例會以下列四種方式格式化值陣列中的 Int32 專案。

int[] numbers = { -1403, 0, 169, 1483104 };
foreach (int number in numbers) {
   // Display value using default formatting.
   Console.Write("{0,-8}  -->   ", number.ToString());
   // Display value with 3 digits and leading zeros.
   Console.Write("{0,11:D3}", number);
   // Display value with 1 decimal digit.
   Console.Write("{0,13:N1}", number);
   // Display value as hexadecimal.
   Console.Write("{0,12:X2}", number);
   // Display value with eight hexadecimal digits.
   Console.WriteLine("{0,14:X8}", number);
}
// The example displays the following output:
//    -1403     -->         -1403     -1,403.0    FFFFFA85      FFFFFA85
//    0         -->           000          0.0          00      00000000
//    169       -->           169        169.0          A9      000000A9
//    1483104   -->       1483104  1,483,104.0      16A160      0016A160
let numbers = [| -1403; 0; 169; 1483104 |]
for number in numbers do
    // Display value using default formatting.
    printf $"{number,-8}  -->   "
    // Display value with 3 digits and leading zeros.
    printf $"{number,11:D3}"
    // Display value with 1 decimal digit.
    printf $"{number,13:N1}"
    // Display value as hexadecimal.
    printf $"{number,12:X2}"
    // Display value with eight hexadecimal digits.
    printfn $"{number,14:X8}"


  // The example displays the following output:
  //    -1403     -->         -1403     -1,403.0    FFFFFA85      FFFFFA85
  //    0         -->           000          0.0          00      00000000
  //    169       -->           169        169.0          A9      000000A9
  //    1483104   -->       1483104  1,483,104.0      16A160      0016A160
Dim numbers() As Integer = { -1403, 0, 169, 1483104 }
For Each number As Integer In numbers
   ' Display value using default formatting.
   Console.Write("{0,-8}  -->   ", number.ToString())
   ' Display value with 3 digits and leading zeros.
   Console.Write("{0,11:D3}", number) 
   ' Display value with 1 decimal digit.
   Console.Write("{0,13:N1}", number) 
   ' Display value as hexadecimal.
   Console.Write("{0,12:X2}", number) 
   ' Display value with eight hexadecimal digits.
   Console.WriteLine("{0,14:X8}", number)
Next   
' The example displays the following output:
'    -1403     -->         -1403     -1,403.0    FFFFFA85      FFFFFA85
'    0         -->           000          0.0          00      00000000
'    169       -->           169        169.0          A9      000000A9
'    1483104   -->       1483104  1,483,104.0      16A160      0016A160

您也可以呼叫 ToString(Int32, Int32) 方法,並提供基底做為方法的第二個參數,將值格式化 Int32 為二進位、八進位、十六進位或十六進位字串。 下列範例會呼叫這個方法,以顯示整數值陣列的二進位、八進位和十六進位標記法。

int[] numbers = { -146, 11043, 2781913 };
Console.WriteLine("{0,8}   {1,32}   {2,11}   {3,10}",
                  "Value", "Binary", "Octal", "Hex");
foreach (int number in numbers) {
   Console.WriteLine("{0,8}   {1,32}   {2,11}   {3,10}",
                     number, Convert.ToString(number, 2),
                     Convert.ToString(number, 8),
                     Convert.ToString(number, 16));
}
// The example displays the following output:
//       Value                             Binary         Octal          Hex
//        -146   11111111111111111111111101101110   37777777556     ffffff6e
//       11043                     10101100100011         25443         2b23
//     2781913             1010100111001011011001      12471331       2a72d9
let numbers = [| -146; 11043; 2781913 |]
printfn $"""{"Value",8}   {"Binary",32}   {"Octal",11}   {"Hex",10}""" 
for number in numbers do
    printfn $"{number,8}   {Convert.ToString(number, 2),32}   {Convert.ToString(number, 8),11}   {Convert.ToString(number, 16),10}"

// The example displays the following output:
//       Value                             Binary         Octal          Hex
//        -146   11111111111111111111111101101110   37777777556     ffffff6e
//       11043                     10101100100011         25443         2b23
//     2781913             1010100111001011011001      12471331       2a72d9
Dim numbers() As Integer = { -146, 11043, 2781913 }
Console.WriteLine("{0,8}   {1,32}   {2,11}   {3,10}", _
                  "Value", "Binary", "Octal", "Hex")
For Each number As Integer In numbers
   Console.WriteLine("{0,8}   {1,32}   {2,11}   {3,10}", _
                     number, Convert.ToString(number, 2), _
                     Convert.ToString(number, 8), _
                     Convert.ToString(number, 16))
Next      
' The example displays the following output:
'       Value                             Binary         Octal          Hex
'        -146   11111111111111111111111101101110   37777777556     ffffff6e
'       11043                     10101100100011         25443         2b23
'     2781913             1010100111001011011001      12471331       2a72d9

使用非十進位 32 位整數值

除了使用個別整數做為十進位值之外,您也可以使用整數值來執行位運算,或使用整數值的二進位或十六進位標記法。 Int32 值以 31 位表示,使用三十秒位做為符號位。 正值是使用正數標記法來表示。 負值在兩個補數標記法中。 當您對 Int32 值執行位運算或使用個別位時,請務必記住這點。 若要在任何兩個非十進位值上執行數值、布林值或比較運算,這兩個值都必須使用相同的標記法。

欄位

MaxValue

代表 Int32 最大的可能值。 這個欄位為常數。

MinValue

代表 Int32 最小的可能值。 這個欄位為常數。

方法

Abs(Int32)

計算值的絕對值。

Clamp(Int32, Int32, Int32)

將值限制為內含最小值和最大值。

CompareTo(Int32)

將這個執行個體與指定的 32 元帶正負號的整數相比較,並傳回它們的相對值指示。

CompareTo(Object)

將這個執行個體與指定的物件相比較,並傳回它們的相對值指示。

CopySign(Int32, Int32)

將值的正負號複製到另一個值的正負號。

CreateChecked<TOther>(TOther)

從值建立目前型別的實例,針對落在目前類型可表示範圍以外的任何值擲回溢位例外狀況。

CreateSaturating<TOther>(TOther)

從值建立目前型別的實例,使落在目前類型可表示範圍以外的任何值飽和。

CreateTruncating<TOther>(TOther)

從值建立目前型別的實例,截斷落在目前類型可表示範圍以外的任何值。

DivRem(Int32, Int32)

計算兩個值的商數和餘數。

Equals(Int32)

傳回值,表示這個執行個體是否等於指定的 Int32 值。

Equals(Object)

傳回值,該值表示這個執行個體是否和指定的物件相等。

GetHashCode()

傳回這個執行個體的雜湊碼。

GetTypeCode()

傳回實值類型 TypeCodeInt32

IsEvenInteger(Int32)

判斷值是否代表偶數整數。

IsNegative(Int32)

判斷值是否為負值。

IsOddInteger(Int32)

判斷值是否代表奇數整數。

IsPositive(Int32)

判斷值是否為正數。

IsPow2(Int32)

判斷值是否為兩個的乘冪。

LeadingZeroCount(Int32)

計算值中的前置零數目。

Log2(Int32)

計算值的 log2。

Max(Int32, Int32)

比較兩個值與大於的計算。

MaxMagnitude(Int32, Int32)

比較兩個值與大於的計算。

Min(Int32, Int32)

比較兩個值與比較較少的計算。

MinMagnitude(Int32, Int32)

比較兩個值與比較較少的計算。

Parse(ReadOnlySpan<Byte>, IFormatProvider)

將 UTF-8 字元的範圍剖析為值。

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

將 UTF-8 字元的範圍剖析為值。

Parse(ReadOnlySpan<Char>, IFormatProvider)

將字元範圍剖析為值。

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

將數字的範圍表示 (使用指定樣式和特定文化特性格式) 轉換為其對等 32 位元帶正負號的整數。

Parse(String)

將數字的字串表示轉換成它的對等 32 位元帶正負號的整數。

Parse(String, IFormatProvider)

將指定特定文化特性格式之數字的字串表示轉換成它的對等 32 位元帶正負號的整數。

Parse(String, NumberStyles)

將指定樣式之數字的字串表示轉換成它的對等 32 位元帶正負號的整數。

Parse(String, NumberStyles, IFormatProvider)

將指定樣式和特定文化特性格式之數字的字串表示轉換成它的對等 32 位元帶正負號的整數。

PopCount(Int32)

計算值中所設定的位數。

RotateLeft(Int32, Int32)

旋轉指定數量所留下的值。

RotateRight(Int32, Int32)

以指定的數量向右旋轉值。

Sign(Int32)

計算值的符號。

ToString()

將這個執行個體的數值轉換為其相等字串表示。

ToString(IFormatProvider)

使用指定的特定文化特性格式資訊,將這個執行個體的數值轉換成它的相等字串表示。

ToString(String)

使用指定格式,將這個執行個體的數值轉換成它的相等字串表示。

ToString(String, IFormatProvider)

使用指定的格式和特定文化特性格式資訊,將這個執行個體的數值轉換成它的相等字串表示。

TrailingZeroCount(Int32)

計算值中尾端零的數目。

TryFormat(Span<Byte>, Int32, ReadOnlySpan<Char>, IFormatProvider)

嘗試將目前實例的值格式化為 UTF-8 到提供的位元組範圍。

TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>, IFormatProvider)

嘗試將目前整數執行個體的值格式化為所提供的字元範圍。

TryParse(ReadOnlySpan<Byte>, IFormatProvider, Int32)

嘗試將 UTF-8 字元的範圍剖析為值。

TryParse(ReadOnlySpan<Byte>, Int32)

嘗試將包含數位之字串表示的 UTF-8 字元範圍轉換為其對等的 32 位帶正負號整數。

TryParse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider, Int32)

嘗試將 UTF-8 字元的範圍剖析為值。

TryParse(ReadOnlySpan<Char>, IFormatProvider, Int32)

嘗試將字元範圍剖析成值。

TryParse(ReadOnlySpan<Char>, Int32)

將數字的範圍表示 (使用指定樣式和特定文化特性格式) 轉換為其對等 32 位元帶正負號的整數。 傳回指示轉換是否成功的值。

TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Int32)

將數字的範圍表示 (使用指定樣式和特定文化特性格式) 轉換為其對等 32 位元帶正負號的整數。 傳回指示轉換是否成功的值。

TryParse(String, IFormatProvider, Int32)

嘗試將字串剖析成值。

TryParse(String, Int32)

將數字的字串表示轉換成它的對等 32 位元帶正負號的整數。 傳回指示轉換是否成功的值。

TryParse(String, NumberStyles, IFormatProvider, Int32)

將指定樣式和特定文化特性格式之數字的字串表示轉換成它的對等 32 位元帶正負號的整數。 傳回指示轉換是否成功的值。

明確介面實作

IAdditionOperators<Int32,Int32,Int32>.Addition(Int32, Int32)

將兩個值加在一起,以計算其總和。

IAdditionOperators<Int32,Int32,Int32>.CheckedAddition(Int32, Int32)

將兩個值加在一起,以計算其總和。

IAdditiveIdentity<Int32,Int32>.AdditiveIdentity

取得目前類型的加法識別。

IBinaryInteger<Int32>.GetByteCount()

取得將寫入為 的 TryWriteLittleEndian(Span<Byte>, Int32) 位元組數目。

IBinaryInteger<Int32>.GetShortestBitLength()

取得目前值之最短兩個補數標記法的長度,以位為單位。

IBinaryInteger<Int32>.TryReadBigEndian(ReadOnlySpan<Byte>, Boolean, Int32)

表示 32 位元帶正負號的整數。

IBinaryInteger<Int32>.TryReadLittleEndian(ReadOnlySpan<Byte>, Boolean, Int32)

表示 32 位元帶正負號的整數。

IBinaryInteger<Int32>.TryWriteBigEndian(Span<Byte>, Int32)

嘗試將目前值以大端格式寫入指定的範圍。

IBinaryInteger<Int32>.TryWriteLittleEndian(Span<Byte>, Int32)

嘗試將目前值以小到尾格式寫入指定的範圍。

IBinaryNumber<Int32>.AllBitsSet

取得二進位類型的實例,其中已設定所有位。

IBitwiseOperators<Int32,Int32,Int32>.BitwiseAnd(Int32, Int32)

計算兩個值的位和 。

IBitwiseOperators<Int32,Int32,Int32>.BitwiseOr(Int32, Int32)

計算兩個值的位或兩個值。

IBitwiseOperators<Int32,Int32,Int32>.ExclusiveOr(Int32, Int32)

計算兩個值的獨佔或兩個值。

IBitwiseOperators<Int32,Int32,Int32>.OnesComplement(Int32)

計算指定值的 ones-補數標記法。

IComparable.CompareTo(Object)

將這個執行個體與指定的物件相比較,並傳回它們的相對值指示。

IComparisonOperators<Int32,Int32,Boolean>.GreaterThan(Int32, Int32)

比較兩個值,以判斷哪一個值更大。

IComparisonOperators<Int32,Int32,Boolean>.GreaterThanOrEqual(Int32, Int32)

比較兩個值,以判斷哪一個值大於或相等。

IComparisonOperators<Int32,Int32,Boolean>.LessThan(Int32, Int32)

比較兩個值,以判斷哪一個值較少。

IComparisonOperators<Int32,Int32,Boolean>.LessThanOrEqual(Int32, Int32)

比較兩個值,以判斷哪一個值小於或相等。

IConvertible.GetTypeCode()

傳回實值類型 TypeCodeInt32

IConvertible.ToBoolean(IFormatProvider)

如需這個成員的說明,請參閱 ToBoolean(IFormatProvider)

IConvertible.ToByte(IFormatProvider)

如需這個成員的說明,請參閱 ToByte(IFormatProvider)

IConvertible.ToChar(IFormatProvider)

如需這個成員的說明,請參閱 ToChar(IFormatProvider)

IConvertible.ToDateTime(IFormatProvider)

不支援此轉換。 嘗試使用這個方法會擲回 InvalidCastException

IConvertible.ToDecimal(IFormatProvider)

如需這個成員的說明,請參閱 ToDecimal(IFormatProvider)

IConvertible.ToDouble(IFormatProvider)

如需這個成員的說明,請參閱 ToDouble(IFormatProvider)

IConvertible.ToInt16(IFormatProvider)

如需這個成員的說明,請參閱 ToInt16(IFormatProvider)

IConvertible.ToInt32(IFormatProvider)

如需這個成員的說明,請參閱 ToInt32(IFormatProvider)

IConvertible.ToInt64(IFormatProvider)

如需這個成員的說明,請參閱 ToInt64(IFormatProvider)

IConvertible.ToSByte(IFormatProvider)

如需這個成員的說明,請參閱 ToSByte(IFormatProvider)

IConvertible.ToSingle(IFormatProvider)

如需這個成員的說明,請參閱 ToSingle(IFormatProvider)

IConvertible.ToType(Type, IFormatProvider)

如需這個成員的說明,請參閱 ToType(Type, IFormatProvider)

IConvertible.ToUInt16(IFormatProvider)

如需這個成員的說明,請參閱 ToUInt16(IFormatProvider)

IConvertible.ToUInt32(IFormatProvider)

如需這個成員的說明,請參閱 ToUInt32(IFormatProvider)

IConvertible.ToUInt64(IFormatProvider)

如需這個成員的說明,請參閱 ToUInt64(IFormatProvider)

IDecrementOperators<Int32>.CheckedDecrement(Int32)

遞減值。

IDecrementOperators<Int32>.Decrement(Int32)

遞減值。

IDivisionOperators<Int32,Int32,Int32>.Division(Int32, Int32)

將一個值除以計算其商數。

IEqualityOperators<Int32,Int32,Boolean>.Equality(Int32, Int32)

比較兩個值來判斷相等。

IEqualityOperators<Int32,Int32,Boolean>.Inequality(Int32, Int32)

比較兩個值來判斷不相等。

IIncrementOperators<Int32>.CheckedIncrement(Int32)

遞增值。

IIncrementOperators<Int32>.Increment(Int32)

遞增值。

IMinMaxValue<Int32>.MaxValue

取得目前型別的最大值。

IMinMaxValue<Int32>.MinValue

取得目前型別的最小值。

IModulusOperators<Int32,Int32,Int32>.Modulus(Int32, Int32)

將兩個值分割在一起,以計算其模數或餘數。

IMultiplicativeIdentity<Int32,Int32>.MultiplicativeIdentity

取得目前型別的乘法識別。

IMultiplyOperators<Int32,Int32,Int32>.CheckedMultiply(Int32, Int32)

將兩個值相乘以計算其產品。

IMultiplyOperators<Int32,Int32,Int32>.Multiply(Int32, Int32)

將兩個值相乘以計算其產品。

INumber<Int32>.MaxNumber(Int32, Int32)

比較兩個值與計算,如果輸入為 NaN ,則會傳回另一個值。

INumber<Int32>.MinNumber(Int32, Int32)

比較兩個值與計算,如果輸入為 NaN ,則會傳回另一個值。

INumberBase<Int32>.IsCanonical(Int32)

判斷值是否在其標準表示中。

INumberBase<Int32>.IsComplexNumber(Int32)

判斷值是否代表複數。

INumberBase<Int32>.IsFinite(Int32)

判斷值是否有限。

INumberBase<Int32>.IsImaginaryNumber(Int32)

判斷值是否代表純虛數。

INumberBase<Int32>.IsInfinity(Int32)

判斷值是否為無限。

INumberBase<Int32>.IsInteger(Int32)

判斷值是否代表整數。

INumberBase<Int32>.IsNaN(Int32)

判斷值是否為 NaN。

INumberBase<Int32>.IsNegativeInfinity(Int32)

判斷值是否為負無限大。

INumberBase<Int32>.IsNormal(Int32)

判斷值是否正常。

INumberBase<Int32>.IsPositiveInfinity(Int32)

判斷值是否為正無限大。

INumberBase<Int32>.IsRealNumber(Int32)

判斷值是否代表實數。

INumberBase<Int32>.IsSubnormal(Int32)

判斷值是否為不正常。

INumberBase<Int32>.IsZero(Int32)

判斷值是否為零。

INumberBase<Int32>.MaxMagnitudeNumber(Int32, Int32)

比較兩個值與具有更大大小的計算,如果輸入為 NaN ,則會傳回另一個值。

INumberBase<Int32>.MinMagnitudeNumber(Int32, Int32)

比較兩個值與計算,如果輸入為 NaN ,則其大小較小,並傳回其他值。

INumberBase<Int32>.One

取得型別的值 1

INumberBase<Int32>.Radix

取得類型的基數或基底。

INumberBase<Int32>.TryConvertFromChecked<TOther>(TOther, Int32)

表示 32 位元帶正負號的整數。

INumberBase<Int32>.TryConvertFromSaturating<TOther>(TOther, Int32)

表示 32 位元帶正負號的整數。

INumberBase<Int32>.TryConvertFromTruncating<TOther>(TOther, Int32)

表示 32 位元帶正負號的整數。

INumberBase<Int32>.TryConvertToChecked<TOther>(Int32, TOther)

嘗試將目前型別的實例轉換成另一個型別,針對落在目前類型可表示範圍以外的任何值擲回溢位例外狀況。

INumberBase<Int32>.TryConvertToSaturating<TOther>(Int32, TOther)

嘗試將目前型別的實例轉換成另一個類型,使任何落在目前類型可表示範圍以外的值飽和。

INumberBase<Int32>.TryConvertToTruncating<TOther>(Int32, TOther)

嘗試將目前型別的實例轉換成另一個型別,並截斷任何落在目前類型之可表示範圍以外的值。

INumberBase<Int32>.Zero

取得型別的值 0

IShiftOperators<Int32,Int32,Int32>.LeftShift(Int32, Int32)

將指定數量留下的值移位。

IShiftOperators<Int32,Int32,Int32>.RightShift(Int32, Int32)

以指定的數量向右移位值。

IShiftOperators<Int32,Int32,Int32>.UnsignedRightShift(Int32, Int32)

以指定的數量向右移位值。

ISignedNumber<Int32>.NegativeOne

取得型別的值 -1

ISubtractionOperators<Int32,Int32,Int32>.CheckedSubtraction(Int32, Int32)

減去兩個值來計算其差異。

ISubtractionOperators<Int32,Int32,Int32>.Subtraction(Int32, Int32)

減去兩個值來計算其差異。

IUnaryNegationOperators<Int32,Int32>.CheckedUnaryNegation(Int32)

計算值的已核取一元負數。

IUnaryNegationOperators<Int32,Int32>.UnaryNegation(Int32)

計算值的一元負數。

IUnaryPlusOperators<Int32,Int32>.UnaryPlus(Int32)

計算值的一元加號。

適用於

執行緒安全性

此類型的所有成員都是安全線程。 看似修改實例狀態的成員,實際上會傳回以新值初始化的新實例。 如同任何其他類型,讀取和寫入包含此類型實例的共用變數必須受到鎖定保護,以確保執行緒安全。

另請參閱