Baca dalam bahasa Inggris

Bagikan melalui


Convert.ToInt64 Metode

Definisi

Mengonversi nilai yang ditentukan menjadi bilangan bulat bertanda tangan 64-bit.

Overload

ToInt64(Single)

Mengonversi nilai dari angka floating-point presisi tunggal yang ditentukan menjadi bilangan bulat bertanda tangan 64-bit yang setara.

ToInt64(String, Int32)

Mengonversi representasi string dari angka dalam basis tertentu menjadi bilangan bulat bertanda tangan 64-bit yang setara.

ToInt64(String, IFormatProvider)

Mengonversi representasi string yang ditentukan dari angka menjadi bilangan bulat bertanda tangan 64-bit yang setara, menggunakan informasi pemformatan khusus budaya yang ditentukan.

ToInt64(Object, IFormatProvider)

Mengonversi nilai objek yang ditentukan menjadi bilangan bulat bertanda tangan 64-bit, menggunakan informasi pemformatan khusus budaya yang ditentukan.

ToInt64(UInt64)

Mengonversi nilai bilangan bulat yang tidak ditandatangani 64-bit yang ditentukan menjadi bilangan bulat bertanda tangan 64-bit yang setara.

ToInt64(UInt32)

Mengonversi nilai bilangan bulat yang tidak ditandatangani 32-bit yang ditentukan menjadi bilangan bulat bertanda tangan 64-bit yang setara.

ToInt64(UInt16)

Mengonversi nilai bilangan bulat yang tidak ditandatangani 16-bit yang ditentukan ke bilangan bulat bertanda tangan 64-bit yang setara.

ToInt64(String)

Mengonversi representasi string yang ditentukan dari angka menjadi bilangan bulat bertanda tangan 64-bit yang setara.

ToInt64(SByte)

Mengonversi nilai bilangan bulat bertanda tangan 8-bit yang ditentukan ke bilangan bulat bertanda tangan 64-bit yang setara.

ToInt64(Object)

Mengonversi nilai objek yang ditentukan menjadi bilangan bulat bertanda tangan 64-bit.

ToInt64(Int64)

Mengembalikan bilangan bulat bertanda tangan 64-bit yang ditentukan; tidak ada konversi aktual yang dilakukan.

ToInt64(Byte)

Mengonversi nilai bilangan bulat yang tidak ditandatangani 8-bit yang ditentukan ke bilangan bulat bertanda tangan 64-bit yang setara.

ToInt64(Char)

Mengonversi nilai karakter Unicode yang ditentukan menjadi bilangan bulat bertanda tangan 64-bit yang setara.

ToInt64(DateTime)

Memanggil metode ini selalu melempar InvalidCastException.

ToInt64(Boolean)

Mengonversi nilai Boolean yang ditentukan ke bilangan bulat bertanda tangan 64-bit yang setara.

ToInt64(Double)

Mengonversi nilai angka floating-point presisi ganda yang ditentukan menjadi bilangan bulat bertanda tangan 64-bit yang setara.

ToInt64(Int16)

Mengonversi nilai bilangan bulat bertanda tangan 16-bit yang ditentukan menjadi bilangan bulat bertanda tangan 64-bit yang setara.

ToInt64(Int32)

Mengonversi nilai bilangan bulat bertanda tangan 32-bit yang ditentukan menjadi bilangan bulat bertanda tangan 64-bit yang setara.

ToInt64(Decimal)

Mengonversi nilai angka desimal yang ditentukan menjadi bilangan bulat bertanda tangan 64-bit yang setara.

ToInt64(Single)

Sumber:
Convert.cs
Sumber:
Convert.cs
Sumber:
Convert.cs

Mengonversi nilai dari angka floating-point presisi tunggal yang ditentukan menjadi bilangan bulat bertanda tangan 64-bit yang setara.

public static long ToInt64 (float value);

Parameter

value
Single

Angka floating-point presisi tunggal untuk dikonversi.

Mengembalikan

value, dibulatkan ke bilangan bulat bertanda 64-bit terdekat. Jika value setengah jalan antara dua bilangan bujur, angka genap dikembalikan; yaitu, 4,5 dikonversi menjadi 4, dan 5,5 dikonversi menjadi 6.

Pengecualian

value lebih besar dari Int64.MaxValue atau kurang dari Int64.MinValue.

Contoh

Contoh berikut mencoba mengonversi setiap elemen dalam array nilai menjadi bilangan Single bulat panjang.

float[] values= { Single.MinValue, -1.38e10f, -1023.299f, -12.98f,
                  0f, 9.113e-16f, 103.919f, 17834.191f, Single.MaxValue };
long result;

foreach (float value in values)
{
   try {
      result = Convert.ToInt64(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 Int64 type.", value);
   }
}
// The example displays the following output:
//    -3.4028235E+38 is outside the range of the Int64 type.
//    Converted the Single value -1.38E+10 to the Int64 value -13799999488.
//    Converted the Single value -1023.299 to the Int64 value -1023.
//    Converted the Single value -12.98 to the Int64 value -13.
//    Converted the Single value 0 to the Int64 value 0.
//    Converted the Single value 9.113E-16 to the Int64 value 0.
//    Converted the Single value 103.919 to the Int64 value 104.
//    Converted the Single value 17834.191 to the Int64 value 17834.
//    3.4028235E+38 is outside the range of the Int64 type.

Lihat juga

Berlaku untuk

.NET 9 dan versi lainnya
Produk Versi
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToInt64(String, Int32)

Sumber:
Convert.cs
Sumber:
Convert.cs
Sumber:
Convert.cs

Mengonversi representasi string dari angka dalam basis tertentu menjadi bilangan bulat bertanda tangan 64-bit yang setara.

public static long ToInt64 (string value, int fromBase);
public static long ToInt64 (string? value, int fromBase);

Parameter

value
String

String yang berisi angka yang akan dikonversi.

fromBase
Int32

Basis angka dalam value, yang harus 2, 8, 10, atau 16.

Mengembalikan

Bilangan bulat bertanda tangan 64-bit yang setara dengan angka di value, atau 0 (nol) jika value adalah null.

Pengecualian

fromBase bukan 2, 8, 10, atau 16.

-atau-

value, yang mewakili angka yang ditandatangani non-dasar 10, diawali dengan tanda negatif.

value berisi karakter yang bukan digit valid dalam basis yang ditentukan oleh fromBase. Pesan pengecualian menunjukkan bahwa tidak ada digit untuk dikonversi jika karakter pertama di value tidak valid; jika tidak, pesan menunjukkan bahwa berisi karakter berikutnya yang value tidak valid.

value, yang mewakili angka yang ditandatangani non-dasar 10, diawali dengan tanda negatif.

-atau-

value mewakili angka yang kurang dari Int64.MinValue atau lebih besar dari Int64.MaxValue.

Keterangan

Jika fromBase adalah 16, Anda dapat mengawali angka yang ditentukan oleh value parameter dengan "0x" atau "0X".

Karena tanda negatif tidak didukung untuk representasi numerik non-basis 10, ToInt64(String, Int32) metode ini mengasumsikan bahwa angka negatif menggunakan representasi pelengkap dua. Dengan kata lain, metode ini selalu menafsirkan bit biner urutan tertinggi dari bilangan bulat panjang (bit 63) sebagai bit tandanya. Akibatnya, dimungkinkan untuk menulis kode di mana angka 10 non-dasar yang berada di luar rentang Int64 jenis data dikonversi ke Int64 nilai tanpa metode melemparkan pengecualian. Contoh berikut mengonversi MaxValue ke representasi string heksadesimalnya, lalu memanggil ToInt64(String, Int32) metode . Alih-alih melempar pengecualian, metode menampilkan pesan, "0xFFFFFFFFFFFFFFFF dikonversi ke -1."

// Create a hexadecimal value out of range of the long type.
string value = ulong.MaxValue.ToString("X");
// Use Convert.ToInt64 to convert it back to a number.
try
{
   long number = Convert.ToInt64(value, 16);
   Console.WriteLine("0x{0} converts to {1}.", value, number);
}
catch (OverflowException)
{
   Console.WriteLine("Unable to convert '0x{0}' to a long integer.", value);
}

Saat melakukan operasi biner atau konversi numerik, selalu merupakan tanggung jawab pengembang untuk memverifikasi bahwa metode menggunakan representasi numerik yang sesuai untuk menafsirkan nilai tertentu. Seperti yang digambarkan contoh berikut, Anda dapat memastikan bahwa metode menangani luapan dengan tepat dengan terlebih dahulu menentukan apakah nilai mewakili jenis yang tidak ditandatangani atau ditandatangani saat mengonversinya ke representasi string heksadesimalnya. Berikan pengecualian jika nilai asli adalah jenis yang tidak ditandatangani tetapi konversi kembali ke bilangan bulat menghasilkan nilai yang bit tandanya aktif.

// Create a negative hexadecimal value out of range of the Byte type.
ulong sourceNumber = ulong.MaxValue;
bool isSigned = Math.Sign(Convert.ToDouble(sourceNumber.GetType().GetField("MinValue").GetValue(null))) == -1;
string value = sourceNumber.ToString("X");
long targetNumber;
try
{
   targetNumber = Convert.ToInt64(value, 16);
   if (! isSigned && ((targetNumber & 0x80000000) != 0))
      throw new OverflowException();
   else
      Console.WriteLine("0x{0} converts to {1}.", value, targetNumber);
}
catch (OverflowException)
{
   Console.WriteLine("Unable to convert '0x{0}' to a long integer.", value);
}
// Displays the following to the console:
//    Unable to convert '0xFFFFFFFFFFFFFFFF' to a long integer.

Contoh berikut mencoba menafsirkan setiap elemen dalam array string sebagai string heksadesimal dan mengonversinya menjadi bilangan bulat panjang.

using System;

public class Example
{
   public static void Main()
   {
      string[] hexStrings = { "8000000000000000", "0FFFFFFFFFFFFFFF",
                              "f0000000000001000", "00A30", "D", "-13", "GAD" };
      foreach (string hexString in hexStrings)
      {
         try {
            long number = Convert.ToInt64(hexString, 16);
            Console.WriteLine("Converted '{0}' to {1:N0}.", hexString, number);
         }
         catch (FormatException) {
            Console.WriteLine("'{0}' is not in the correct format for a hexadecimal number.",
                              hexString);
         }
         catch (OverflowException) {
            Console.WriteLine("'{0}' is outside the range of an Int64.", hexString);
         }
         catch (ArgumentException) {
            Console.WriteLine("'{0}' is invalid in base 16.", hexString);
         }
      }
   }
}
// The example displays the following output:
//       Converted '8000000000000000' to -9,223,372,036,854,775,808.
//       Converted '0FFFFFFFFFFFFFFF' to 1,152,921,504,606,846,975.
//       'f0000000000001000' is outside the range of an Int64.
//       Converted '00A30' to 2,608.
//       Converted 'D' to 13.
//       '-13' is invalid in base 16.
//       'GAD' is not in the correct format for a hexadecimal number.

Berlaku untuk

.NET 9 dan versi lainnya
Produk Versi
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToInt64(String, IFormatProvider)

Sumber:
Convert.cs
Sumber:
Convert.cs
Sumber:
Convert.cs

Mengonversi representasi string yang ditentukan dari angka menjadi bilangan bulat bertanda tangan 64-bit yang setara, menggunakan informasi pemformatan khusus budaya yang ditentukan.

public static long ToInt64 (string value, IFormatProvider provider);
public static long ToInt64 (string? value, IFormatProvider? provider);

Parameter

value
String

String yang berisi angka yang akan dikonversi.

provider
IFormatProvider

Objek yang memasok informasi pemformatan khusus budaya.

Mengembalikan

Bilangan bulat bertanda tangan 64-bit yang setara dengan angka di value, atau 0 (nol) jika value adalah null.

Pengecualian

value tidak terdiri dari tanda opsional diikuti dengan urutan digit (0 hingga 9).

value mewakili angka yang kurang dari Int64.MinValue atau lebih besar dari Int64.MaxValue.

Contoh

Contoh berikut mendefinisikan objek kustom NumberFormatInfo yang mengenali string "pos" sebagai tanda positif dan string "neg" sebagai tanda negatif. Kemudian mencoba untuk mengonversi setiap elemen array string numerik menjadi bilangan bulat menggunakan penyedia ini dan NumberFormatInfo penyedia untuk budaya invarian.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      // Create a NumberFormatInfo object and set the properties that
      // affect conversions using Convert.ToInt64(String, IFormatProvider).
      NumberFormatInfo customProvider = new NumberFormatInfo();
      customProvider.NegativeSign = "neg ";
      customProvider.PositiveSign = "pos ";

      // Create an array of providers with the custom provider and the
      // NumberFormatInfo object for the invariant culture.
      NumberFormatInfo[] providers = { customProvider,
                                       NumberFormatInfo.InvariantInfo };

      // Define an array of strings to parse.
      string[] numericStrings = { "123456789", "+123456789", "pos 123456789",
                                  "-123456789", "neg 123456789", "123456789.",
                                  "123,456,789", "(123456789)",
                                  "9223372036854775808", "-9223372036854775809" };

      for (int ctr = 0; ctr < 2; ctr++)
      {
         IFormatProvider provider = providers[ctr];
         Console.WriteLine(ctr == 0 ? "Custom Provider:" : "Invariant Culture:");
         foreach (string numericString in numericStrings)
         {
            Console.Write("   {0,-22} -->  ", numericString);
            try {
               Console.WriteLine("{0,22}", Convert.ToInt32(numericString, provider));
            }
            catch (FormatException) {
               Console.WriteLine("{0,22}", "Unrecognized Format");
            }
            catch (OverflowException) {
               Console.WriteLine("{0,22}", "Overflow");
            }
         }
         Console.WriteLine();
      }
   }
}
// The example displays the following output:
//       Custom Provider:
//          123456789              -->               123456789
//          +123456789             -->     Unrecognized Format
//          pos 123456789          -->               123456789
//          -123456789             -->     Unrecognized Format
//          neg 123456789          -->              -123456789
//          123456789.             -->     Unrecognized Format
//          123,456,789            -->     Unrecognized Format
//          (123456789)            -->     Unrecognized Format
//          9223372036854775808    -->                Overflow
//          -9223372036854775809   -->     Unrecognized Format
//
//       Invariant Culture:
//          123456789              -->               123456789
//          +123456789             -->               123456789
//          pos 123456789          -->     Unrecognized Format
//          -123456789             -->              -123456789
//          neg 123456789          -->     Unrecognized Format
//          123456789.             -->     Unrecognized Format
//          123,456,789            -->     Unrecognized Format
//          (123456789)            -->     Unrecognized Format
//          9223372036854775808    -->                Overflow
//          -9223372036854775809   -->                Overflow

Keterangan

Nilai yang dikembalikan adalah hasil dari pemanggilan Int64.Parse metode pada value.

provider IFormatProvider adalah instans NumberFormatInfo yang mendapatkan objek . Objek menyediakan NumberFormatInfo informasi khusus budaya tentang format value. Jika provider adalah null, NumberFormatInfo untuk budaya saat ini digunakan.

Jika Anda lebih suka tidak menangani pengecualian jika konversi gagal, Anda dapat memanggil metode sebagai gantinya Int64.TryParse . Ini mengembalikan Boolean nilai yang menunjukkan apakah konversi berhasil atau gagal.

Berlaku untuk

.NET 9 dan versi lainnya
Produk Versi
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToInt64(Object, IFormatProvider)

Sumber:
Convert.cs
Sumber:
Convert.cs
Sumber:
Convert.cs

Mengonversi nilai objek yang ditentukan menjadi bilangan bulat bertanda tangan 64-bit, menggunakan informasi pemformatan khusus budaya yang ditentukan.

public static long ToInt64 (object value, IFormatProvider provider);
public static long ToInt64 (object? value, IFormatProvider? provider);

Parameter

value
Object

Objek yang mengimplementasikan IConvertible antarmuka.

provider
IFormatProvider

Objek yang memasok informasi pemformatan khusus budaya.

Mengembalikan

Bilangan bulat bertanda tangan 64-bit yang setara valuedengan , atau nol jika value adalah null.

Pengecualian

value tidak dalam format yang sesuai.

value tidak mengimplementasikan IConvertible antarmuka.

-atau-

Konversi tidak didukung.

value mewakili angka yang kurang dari Int64.MinValue atau lebih besar dari Int64.MaxValue.

Contoh

Contoh berikut mendefinisikan kelas yang mengimplementasikan IConvertible dan kelas yang mengimplementasikan IFormatProvider. Objek kelas yang mengimplementasikan IConvertible menyimpan array Double nilai. Objek dari setiap kelas diteruskan ke ToInt64 metode . Metode ini mengembalikan rata-rata array Double nilai, menggunakan objek yang mengimplementasikan IFormatProvider untuk menentukan cara menghitung rata-rata.

using System;
using System.Collections;

// Define the types of averaging available in the class
// implementing IConvertible.
public enum AverageType : short
{
    None = 0,
    GeometricMean = 1,
    ArithmeticMean = 2,
    Median = 3
};

// Pass an instance of this class to methods that require an
// IFormatProvider. The class instance determines the type of
// average to calculate.
public class AverageInfo : IFormatProvider
{
    protected AverageType AvgType;

    // Specify the type of averaging in the constructor.
    public AverageInfo( AverageType avgType )
    {
        this.AvgType = avgType;
    }

    // This method returns a reference to the containing object
    // if an object of AverageInfo type is requested.
    public object GetFormat( Type argType )
    {
        if ( argType == typeof( AverageInfo ) )
            return this;
        else
            return null;
    }

    // Use this property to set or get the type of averaging.
    public AverageType TypeOfAverage
    {
        get { return this.AvgType; }
        set { this.AvgType = value; }
    }
}

// This class encapsulates an array of double values and implements
// the IConvertible interface. Most of the IConvertible methods
// return an average of the array elements in one of three types:
// arithmetic mean, geometric mean, or median.
public class DataSet : IConvertible
{
    protected ArrayList     data;
    protected AverageInfo   defaultProvider;

    // Construct the object and add an initial list of values.
    // Create a default format provider.
    public DataSet( params double[ ] values )
    {
        data = new ArrayList( values );
        defaultProvider =
            new AverageInfo( AverageType.ArithmeticMean );
    }

    // Add additional values with this method.
    public int Add( double value )
    {
        data.Add( value );
        return data.Count;
    }

    // Get, set, and add values with this indexer property.
    public double this[ int index ]
    {
        get
        {
            if( index >= 0 && index < data.Count )
                return (double)data[ index ];
            else
                throw new InvalidOperationException(
                    "[DataSet.get] Index out of range." );
        }
        set
        {
            if( index >= 0 && index < data.Count )
                data[ index ] = value;

            else if( index == data.Count )
                data.Add( value );
            else
                throw new InvalidOperationException(
                    "[DataSet.set] Index out of range." );
        }
    }

    // This property returns the number of elements in the object.
    public int Count
    {
        get { return data.Count; }
    }

    // This method calculates the average of the object's elements.
    protected double Average( AverageType avgType )
    {
        double  SumProd;

        if( data.Count == 0 )
            return 0.0;

        switch( avgType )
        {
            case AverageType.GeometricMean:

                SumProd = 1.0;
                for( int Index = 0; Index < data.Count; Index++ )
                    SumProd *= (double)data[ Index ];

                // This calculation will not fail with negative
                // elements.
                return Math.Sign( SumProd ) * Math.Pow(
                    Math.Abs( SumProd ), 1.0 / data.Count );

            case AverageType.ArithmeticMean:

                SumProd = 0.0;
                for( int Index = 0; Index < data.Count; Index++ )
                    SumProd += (double)data[ Index ];

                return SumProd / data.Count;

            case AverageType.Median:

                if( data.Count % 2 == 0 )
                    return ( (double)data[ data.Count / 2 ] +
                        (double)data[ data.Count / 2 - 1 ] ) / 2.0;
                else
                    return (double)data[ data.Count / 2 ];

            default:
                return 0.0;
        }
    }

    // Get the AverageInfo object from the caller's format provider,
    // or use the local default.
    protected AverageInfo GetAverageInfo( IFormatProvider provider )
    {
        AverageInfo avgInfo = null;

        if( provider != null )
            avgInfo = (AverageInfo)provider.GetFormat(
                typeof( AverageInfo ) );

        if ( avgInfo == null )
            return defaultProvider;
        else
            return avgInfo;
    }

    // Calculate the average and limit the range.
    protected double CalcNLimitAverage( double min, double max,
        IFormatProvider provider )
    {
        // Get the format provider and calculate the average.
        AverageInfo avgInfo = GetAverageInfo( provider );
        double avg = Average( avgInfo.TypeOfAverage );

        // Limit the range, based on the minimum and maximum values
        // for the type.
        return avg > max ? max : avg < min ? min : avg;
    }

    // The following elements are required by IConvertible.

    // None of these conversion functions throw exceptions. When
    // the data is out of range for the type, the appropriate
    // MinValue or MaxValue is used.
    public TypeCode GetTypeCode( )
    {
        return TypeCode.Object;
    }

    public bool ToBoolean( IFormatProvider provider )
    {
        // ToBoolean is false if the dataset is empty.
        if( data.Count <= 0 )
        {
            return false;
        }

        // For median averaging, ToBoolean is true if any
        // non-discarded elements are nonzero.
        else if( AverageType.Median ==
            GetAverageInfo( provider ).TypeOfAverage )
        {
            if (data.Count % 2 == 0 )
                return ( (double)data[ data.Count / 2 ] != 0.0 ||
                    (double)data[ data.Count / 2 - 1 ] != 0.0 );
            else
                return (double)data[ data.Count / 2 ] != 0.0;
        }

        // For arithmetic or geometric mean averaging, ToBoolean is
        // true if any element of the dataset is nonzero.
        else
        {
            for( int Index = 0; Index < data.Count; Index++ )
                if( (double)data[ Index ] != 0.0 )
                    return true;
            return false;
        }
    }

    public byte ToByte( IFormatProvider provider )
    {
        return Convert.ToByte( CalcNLimitAverage(
            Byte.MinValue, Byte.MaxValue, provider ) );
    }

    public char ToChar( IFormatProvider provider )
    {
        return Convert.ToChar( Convert.ToUInt16( CalcNLimitAverage(
            Char.MinValue, Char.MaxValue, provider ) ) );
    }

    // Convert to DateTime by adding the calculated average as
    // seconds to the current date and time. A valid DateTime is
    // always returned.
    public DateTime ToDateTime( IFormatProvider provider )
    {
        double seconds =
            Average( GetAverageInfo( provider ).TypeOfAverage );
        try
        {
            return DateTime.Now.AddSeconds( seconds );
        }
        catch( ArgumentOutOfRangeException )
        {
            return seconds < 0.0 ? DateTime.MinValue : DateTime.MaxValue;
        }
    }

    public decimal ToDecimal( IFormatProvider provider )
    {
        // The Double conversion rounds Decimal.MinValue and
        // Decimal.MaxValue to invalid Decimal values, so the
        // following limits must be used.
        return Convert.ToDecimal( CalcNLimitAverage(
            -79228162514264330000000000000.0,
            79228162514264330000000000000.0, provider ) );
    }

    public double ToDouble( IFormatProvider provider )
    {
        return Average( GetAverageInfo(provider).TypeOfAverage );
    }

    public short ToInt16( IFormatProvider provider )
    {
        return Convert.ToInt16( CalcNLimitAverage(
            Int16.MinValue, Int16.MaxValue, provider ) );
    }

    public int ToInt32( IFormatProvider provider )
    {
        return Convert.ToInt32( CalcNLimitAverage(
            Int32.MinValue, Int32.MaxValue, provider ) );
    }

    public long ToInt64( IFormatProvider provider )
    {
        // The Double conversion rounds Int64.MinValue and
        // Int64.MaxValue to invalid Int64 values, so the following
        // limits must be used.
        return Convert.ToInt64( CalcNLimitAverage(
            -9223372036854775000, 9223372036854775000, provider ) );
    }

    public SByte ToSByte( IFormatProvider provider )
    {
        return Convert.ToSByte( CalcNLimitAverage(
            SByte.MinValue, SByte.MaxValue, provider ) );
    }

    public float ToSingle( IFormatProvider provider )
    {
        return Convert.ToSingle( CalcNLimitAverage(
            Single.MinValue, Single.MaxValue, provider ) );
    }

    public UInt16 ToUInt16( IFormatProvider provider )
    {
        return Convert.ToUInt16( CalcNLimitAverage(
            UInt16.MinValue, UInt16.MaxValue, provider ) );
    }

    public UInt32 ToUInt32( IFormatProvider provider )
    {
        return Convert.ToUInt32( CalcNLimitAverage(
            UInt32.MinValue, UInt32.MaxValue, provider ) );
    }

    public UInt64 ToUInt64( IFormatProvider provider )
    {
        // The Double conversion rounds UInt64.MaxValue to an invalid
        // UInt64 value, so the following limit must be used.
        return Convert.ToUInt64( CalcNLimitAverage(
            0, 18446744073709550000.0, provider ) );
    }

    public object ToType( Type conversionType,
        IFormatProvider provider )
    {
        return Convert.ChangeType( Average(
            GetAverageInfo( provider ).TypeOfAverage ),
            conversionType );
    }

    public string ToString( IFormatProvider provider )
    {
        AverageType avgType = GetAverageInfo( provider ).TypeOfAverage;
        return String.Format( "( {0}: {1:G10} )", avgType,
            Average( avgType ) );
    }
}

class IConvertibleProviderDemo
{
    // Display a DataSet with three different format providers.
    public static void DisplayDataSet( DataSet ds )
    {
        string      fmt    = "{0,-12}{1,20}{2,20}{3,20}";
        AverageInfo median = new AverageInfo( AverageType.Median );
        AverageInfo geMean =
            new AverageInfo( AverageType.GeometricMean );

         // Display the dataset elements.
        if( ds.Count > 0 )
        {
            Console.Write( "\nDataSet: [{0}", ds[ 0 ] );
            for( int iX = 1; iX < ds.Count; iX++ )
                Console.Write( ", {0}", ds[ iX ] );
            Console.WriteLine( "]\n" );
        }

        Console.WriteLine( fmt, "Convert.", "Default",
            "Geometric Mean", "Median");
        Console.WriteLine( fmt, "--------", "-------",
            "--------------", "------");
        Console.WriteLine( fmt, "ToBoolean",
            Convert.ToBoolean( ds, null ),
            Convert.ToBoolean( ds, geMean ),
            Convert.ToBoolean( ds, median ) );
        Console.WriteLine( fmt, "ToByte",
            Convert.ToByte( ds, null ),
            Convert.ToByte( ds, geMean ),
            Convert.ToByte( ds, median ) );
        Console.WriteLine( fmt, "ToChar",
            Convert.ToChar( ds, null ),
            Convert.ToChar( ds, geMean ),
            Convert.ToChar( ds, median ) );
        Console.WriteLine( "{0,-12}{1,20:yyyy-MM-dd HH:mm:ss}" +
            "{2,20:yyyy-MM-dd HH:mm:ss}{3,20:yyyy-MM-dd HH:mm:ss}",
            "ToDateTime", Convert.ToDateTime( ds, null ),
            Convert.ToDateTime( ds, geMean ),
            Convert.ToDateTime( ds, median ) );
        Console.WriteLine( fmt, "ToDecimal",
            Convert.ToDecimal( ds, null ),
            Convert.ToDecimal( ds, geMean ),
            Convert.ToDecimal( ds, median ) );
        Console.WriteLine( fmt, "ToDouble",
            Convert.ToDouble( ds, null ),
            Convert.ToDouble( ds, geMean ),
            Convert.ToDouble( ds, median ) );
        Console.WriteLine( fmt, "ToInt16",
            Convert.ToInt16( ds, null ),
            Convert.ToInt16( ds, geMean ),
            Convert.ToInt16( ds, median ) );
        Console.WriteLine( fmt, "ToInt32",
            Convert.ToInt32( ds, null ),
            Convert.ToInt32( ds, geMean ),
            Convert.ToInt32( ds, median ) );
        Console.WriteLine( fmt, "ToInt64",
            Convert.ToInt64( ds, null ),
            Convert.ToInt64( ds, geMean ),
            Convert.ToInt64( ds, median ) );
        Console.WriteLine( fmt, "ToSByte",
            Convert.ToSByte( ds, null ),
            Convert.ToSByte( ds, geMean ),
            Convert.ToSByte( ds, median ) );
        Console.WriteLine( fmt, "ToSingle",
            Convert.ToSingle( ds, null ),
            Convert.ToSingle( ds, geMean ),
            Convert.ToSingle( ds, median ) );
        Console.WriteLine( fmt, "ToUInt16",
            Convert.ToUInt16( ds, null ),
            Convert.ToUInt16( ds, geMean ),
            Convert.ToUInt16( ds, median ) );
        Console.WriteLine( fmt, "ToUInt32",
            Convert.ToUInt32( ds, null ),
            Convert.ToUInt32( ds, geMean ),
            Convert.ToUInt32( ds, median ) );
        Console.WriteLine( fmt, "ToUInt64",
            Convert.ToUInt64( ds, null ),
            Convert.ToUInt64( ds, geMean ),
            Convert.ToUInt64( ds, median ) );
    }

    public static void Main( )
    {
        Console.WriteLine( "This example of " +
            "the Convert.To<Type>( object, IFormatProvider ) methods " +
            "\ngenerates the following output. The example " +
            "displays the values \nreturned by the methods, " +
            "using several IFormatProvider objects.\n" );

        DataSet ds1 = new DataSet(
            10.5, 22.2, 45.9, 88.7, 156.05, 297.6 );
        DisplayDataSet( ds1 );

        DataSet ds2 = new DataSet(
            359999.95, 425000, 499999.5, 775000, 1695000 );
        DisplayDataSet( ds2 );
    }
}

/*
This example of the Convert.To<Type>( object, IFormatProvider ) methods
generates the following output. The example displays the values
returned by the methods, using several IFormatProvider objects.

DataSet: [10.5, 22.2, 45.9, 88.7, 156.05, 297.6]

Convert.                 Default      Geometric Mean              Median
--------                 -------      --------------              ------
ToBoolean                   True                True                True
ToByte                       103                  59                  67
ToChar                         g                   ;                   C
ToDateTime   2003-05-13 15:04:12 2003-05-13 15:03:28 2003-05-13 15:03:35
ToDecimal       103.491666666667    59.4332135445164                67.3
ToDouble        103.491666666667    59.4332135445164                67.3
ToInt16                      103                  59                  67
ToInt32                      103                  59                  67
ToInt64                      103                  59                  67
ToSByte                      103                  59                  67
ToSingle                103.4917            59.43321                67.3
ToUInt16                     103                  59                  67
ToUInt32                     103                  59                  67
ToUInt64                     103                  59                  67

DataSet: [359999.95, 425000, 499999.5, 775000, 1695000]

Convert.                 Default      Geometric Mean              Median
--------                 -------      --------------              ------
ToBoolean                   True                True                True
ToByte                       255                 255                 255
ToChar                         ?                   ?                   ?
ToDateTime   2003-05-22 07:39:08 2003-05-20 22:28:45 2003-05-19 09:55:48
ToDecimal              750999.89    631577.237188435            499999.5
ToDouble               750999.89    631577.237188435            499999.5
ToInt16                    32767               32767               32767
ToInt32                   751000              631577              500000
ToInt64                   751000              631577              500000
ToSByte                      127                 127                 127
ToSingle                750999.9            631577.3            499999.5
ToUInt16                   65535               65535               65535
ToUInt32                  751000              631577              500000
ToUInt64                  751000              631577              500000
*/

Keterangan

Nilai yang dikembalikan adalah hasil dari pemanggilan IConvertible.ToInt64 metode dari jenis yang mendasar dari value.

provider memungkinkan pengguna untuk menentukan informasi konversi khusus budaya tentang konten value. Misalnya, jika value adalah String yang mewakili angka, provider dapat menyediakan informasi khusus budaya tentang notasi yang digunakan untuk mewakili angka tersebut.

Jenis dasar mengabaikan provider; namun, parameter dapat digunakan jika value adalah jenis yang ditentukan pengguna yang mengimplementasikan IConvertible antarmuka.

Berlaku untuk

.NET 9 dan versi lainnya
Produk Versi
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToInt64(UInt64)

Sumber:
Convert.cs
Sumber:
Convert.cs
Sumber:
Convert.cs

Penting

API ini bukan kompatibel CLS.

Mengonversi nilai bilangan bulat yang tidak ditandatangani 64-bit yang ditentukan menjadi bilangan bulat bertanda tangan 64-bit yang setara.

[System.CLSCompliant(false)]
public static long ToInt64 (ulong value);

Parameter

value
UInt64

Bilangan bulat tidak bertanda 64-bit untuk dikonversi.

Mengembalikan

Bilangan bulat bertanda tangan 64-bit yang setara dengan value.

Atribut

Pengecualian

value lebih besar dari Int64.MaxValue.

Contoh

Contoh berikut mencoba mengonversi setiap elemen dalam array bilangan bulat panjang yang tidak ditandatangani menjadi bilangan bulat panjang.

ulong[] numbers = { UInt64.MinValue, 121, 340, UInt64.MaxValue };
long result;
foreach (ulong number in numbers)
{
   try {
      result = Convert.ToInt64(number);
      Console.WriteLine("Converted the {0} value {1} to a {2} value {3}.",
                        number.GetType().Name, number,
                        result.GetType().Name, result);
   }
   catch (OverflowException) {
      Console.WriteLine("The {0} value {1} is outside the range of the Int64 type.",
                        number.GetType().Name, number);
   }
}
// The example displays the following output:
//    Converted the UInt64 value 0 to a Int32 value 0.
//    Converted the UInt64 value 121 to a Int32 value 121.
//    Converted the UInt64 value 340 to a Int32 value 340.
//    The UInt64 value 18446744073709551615 is outside the range of the Int64 type.

Berlaku untuk

.NET 9 dan versi lainnya
Produk Versi
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToInt64(UInt32)

Sumber:
Convert.cs
Sumber:
Convert.cs
Sumber:
Convert.cs

Penting

API ini bukan kompatibel CLS.

Mengonversi nilai bilangan bulat yang tidak ditandatangani 32-bit yang ditentukan menjadi bilangan bulat bertanda tangan 64-bit yang setara.

[System.CLSCompliant(false)]
public static long ToInt64 (uint value);

Parameter

value
UInt32

Bilangan bulat tidak bertanda 32-bit untuk dikonversi.

Mengembalikan

Bilangan bulat bertanda tangan 64-bit yang setara dengan value.

Atribut

Contoh

Contoh berikut mengonversi setiap elemen dalam array bilangan bulat yang tidak ditandatangani menjadi bilangan bulat panjang.

uint[] numbers = { UInt32.MinValue, 121, 340, UInt32.MaxValue };
long result;
foreach (uint number in numbers)
{
   result = Convert.ToInt64(number);
   Console.WriteLine("Converted the {0} value {1:N0} to the {2} value {3:N0}.",
                     number.GetType().Name, number,
                     result.GetType().Name, result);
}
// The example displays the following output:
//    Converted the UInt32 value 0 to the Int64 value 0.
//    Converted the UInt32 value 121 to the Int64 value 121.
//    Converted the UInt32 value 340 to the Int64 value 340.
//    Converted the UInt32 value 4,294,967,295 to the Int64 value 4,294,967,295.

Berlaku untuk

.NET 9 dan versi lainnya
Produk Versi
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToInt64(UInt16)

Sumber:
Convert.cs
Sumber:
Convert.cs
Sumber:
Convert.cs

Penting

API ini bukan kompatibel CLS.

Mengonversi nilai bilangan bulat yang tidak ditandatangani 16-bit yang ditentukan ke bilangan bulat bertanda tangan 64-bit yang setara.

[System.CLSCompliant(false)]
public static long ToInt64 (ushort value);

Parameter

value
UInt16

Bilangan bulat yang tidak ditandatangani 16-bit untuk dikonversi.

Mengembalikan

Bilangan bulat bertanda tangan 64-bit yang setara dengan value.

Atribut

Contoh

Contoh berikut mengonversi setiap elemen dalam array bilangan bulat yang tidak ditandatangani 16-bit menjadi bilangan bulat panjang.

ushort[] numbers = { UInt16.MinValue, 121, 340, UInt16.MaxValue };
long result;
foreach (ushort number in numbers)
{
   try {
      result = Convert.ToInt64(number);
      Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.",
                        number.GetType().Name, number,
                        result.GetType().Name, result);
   }
   catch (OverflowException) {
      Console.WriteLine("The {0} value {1} is outside the range of the Int64 type.",
                        number.GetType().Name, number);
   }
}
// The example displays the following output:
//    Converted the UInt16 value 0 to the Int64 value 0.
//    Converted the UInt16 value 121 to the Int64 value 121.
//    Converted the UInt16 value 340 to the Int64 value 340.
//    Converted the UInt16 value 65535 to the Int64 value 65535.

Berlaku untuk

.NET 9 dan versi lainnya
Produk Versi
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToInt64(String)

Sumber:
Convert.cs
Sumber:
Convert.cs
Sumber:
Convert.cs

Mengonversi representasi string yang ditentukan dari angka menjadi bilangan bulat bertanda tangan 64-bit yang setara.

public static long ToInt64 (string value);
public static long ToInt64 (string? value);

Parameter

value
String

String yang berisi angka yang akan dikonversi.

Mengembalikan

Bilangan bulat bertanda tangan 64-bit yang setara dengan angka di value, atau 0 (nol) jika value adalah null.

Pengecualian

value tidak terdiri dari tanda opsional diikuti dengan urutan digit (0 hingga 9).

value mewakili angka yang kurang dari Int64.MinValue atau lebih besar dari Int64.MaxValue.

Contoh

Contoh berikut mencoba mengonversi setiap elemen dalam array string numerik menjadi bilangan bulat panjang.

string[] values = { "One", "1.34e28", "-26.87", "-18", "-6.00",
                    " 0", "137", "1601.9", Int32.MaxValue.ToString() };
long result;

foreach (string value in values)
{
   try {
      result = Convert.ToInt64(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 Int64 type.", value);
   }
   catch (FormatException) {
      Console.WriteLine("The {0} value '{1}' is not in a recognizable format.",
                        value.GetType().Name, value);
   }
}
// The example displays the following output:
//    The String value 'One' is not in a recognizable format.
//    The String value '1.34e28' is not in a recognizable format.
//    The String value '-26.87' is not in a recognizable format.
//    Converted the String value '-18' to the Int64 value -18.
//    The String value '-6.00' is not in a recognizable format.
//    Converted the String value ' 0' to the Int64 value 0.
//    Converted the String value '137' to the Int64 value 137.
//    The String value '1601.9' is not in a recognizable format.
//    Converted the String value '2147483647' to the Int64 value 2147483647.

Keterangan

ToInt64(String) Menggunakan metode ini setara dengan meneruskan value ke Int64.Parse(String) metode . value ditafsirkan dengan menggunakan konvensi pemformatan dari budaya saat ini.

Jika Anda lebih suka tidak menangani pengecualian jika konversi gagal, Anda dapat memanggil metode sebagai gantinya Int64.TryParse . Ini mengembalikan Boolean nilai yang menunjukkan apakah konversi berhasil atau gagal.

Berlaku untuk

.NET 9 dan versi lainnya
Produk Versi
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToInt64(SByte)

Sumber:
Convert.cs
Sumber:
Convert.cs
Sumber:
Convert.cs

Penting

API ini bukan kompatibel CLS.

Mengonversi nilai bilangan bulat bertanda tangan 8-bit yang ditentukan ke bilangan bulat bertanda tangan 64-bit yang setara.

[System.CLSCompliant(false)]
public static long ToInt64 (sbyte value);

Parameter

value
SByte

Bilangan bulat bertanda tangan 8-bit untuk dikonversi.

Mengembalikan

Bilangan bulat bertanda tangan 64-bit yang setara dengan value.

Atribut

Contoh

Contoh berikut mengonversi setiap elemen dalam array byte yang ditandatangani menjadi bilangan bulat panjang.

sbyte[] numbers = { SByte.MinValue, -1, 0, 10, SByte.MaxValue };
long result;

foreach (sbyte number in numbers)
{
   result = Convert.ToInt64(number);
   Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.",
                     number.GetType().Name, number,
                     result.GetType().Name, result);
}
// The example displays the following output:
//       Converted the SByte value -128 to the Int64 value -128.
//       Converted the SByte value -1 to the Int64 value -1.
//       Converted the SByte value 0 to the Int64 value 0.
//       Converted the SByte value 10 to the Int64 value 10.
//       Converted the SByte value 127 to the Int64 value 127.

Berlaku untuk

.NET 9 dan versi lainnya
Produk Versi
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToInt64(Object)

Sumber:
Convert.cs
Sumber:
Convert.cs
Sumber:
Convert.cs

Mengonversi nilai objek yang ditentukan menjadi bilangan bulat bertanda tangan 64-bit.

public static long ToInt64 (object value);
public static long ToInt64 (object? value);

Parameter

value
Object

Objek yang mengimplementasikan IConvertible antarmuka, atau null.

Mengembalikan

Bilangan bulat bertanda tangan 64-bit yang setara valuedengan , atau nol jika value adalah null.

Pengecualian

value tidak dalam format yang sesuai.

value tidak mengimplementasikan IConvertible antarmuka.

-atau-

Konversi tidak didukung.

value mewakili angka yang kurang dari Int64.MinValue atau lebih besar dari Int64.MaxValue.

Contoh

Contoh berikut mencoba mengonversi setiap elemen dalam array objek menjadi bilangan bulat panjang.

object[] values = { true, -12, 163, 935, 'x', new DateTime(2009, 5, 12),
                    "104", "103.0", "-1",
                    "1.00e2", "One", 1.00e2, 16.3e42};
long result;

foreach (object value in values)
{
   try {
      result = Convert.ToInt64(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("The {0} value {1} is outside the range of the Int64 type.",
                        value.GetType().Name, value);
   }
   catch (FormatException) {
      Console.WriteLine("The {0} value {1} is not in a recognizable format.",
                        value.GetType().Name, value);
   }
   catch (InvalidCastException) {
      Console.WriteLine("No conversion to an Int64 exists for the {0} value {1}.",
                        value.GetType().Name, value);
   }
}
// The example displays the following output:
//    Converted the Boolean value True to the Int64 value 1.
//    Converted the Int32 value -12 to the Int64 value -12.
//    Converted the Int32 value 163 to the Int64 value 163.
//    Converted the Int32 value 935 to the Int64 value 935.
//    Converted the Char value x to the Int64 value 120.
//    No conversion to an Int64 exists for the DateTime value 5/12/2009 12:00:00 AM.
//    Converted the String value 104 to the Int64 value 104.
//    The String value 103.0 is not in a recognizable format.
//    Converted the String value -1 to the Int64 value -1.
//    The String value 1.00e2 is not in a recognizable format.
//    The String value One is not in a recognizable format.
//    Converted the Double value 100 to the Int64 value 100.
//    The Double value 1.63E+43 is outside the range of the Int64 type.

Keterangan

Jika value tidak null, metode ini membungkus panggilan ke IConvertible.ToInt64 implementasi jenis yang mendasar dari value.

Berlaku untuk

.NET 9 dan versi lainnya
Produk Versi
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToInt64(Int64)

Sumber:
Convert.cs
Sumber:
Convert.cs
Sumber:
Convert.cs

Mengembalikan bilangan bulat bertanda tangan 64-bit yang ditentukan; tidak ada konversi aktual yang dilakukan.

public static long ToInt64 (long value);

Parameter

value
Int64

Bilangan bulat bertanda 64-bit.

Mengembalikan

value dikembalikan tidak berubah.

Contoh

Contoh berikut mengonversi setiap elemen dalam array bilangan bulat menjadi bilangan bulat panjang.

int[] numbers = { Int32.MinValue, -1, 0, 121, 340, Int32.MaxValue };
long result;
foreach (int number in numbers)
{
   result = Convert.ToInt64(number);
   Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.",
                     number.GetType().Name, number,
                     result.GetType().Name, result);
}
// The example displays the following output:
//    Converted the Int32 value -2147483648 to the Int64 value -2147483648.
//    Converted the Int32 value -1 to the Int64 value -1.
//    Converted the Int32 value 0 to the Int64 value 0.
//    Converted the Int32 value 121 to the Int64 value 121.
//    Converted the Int32 value 340 to the Int64 value 340.
//    Converted the Int32 value 2147483647 to the Int64 value 2147483647.

Berlaku untuk

.NET 9 dan versi lainnya
Produk Versi
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToInt64(Byte)

Sumber:
Convert.cs
Sumber:
Convert.cs
Sumber:
Convert.cs

Mengonversi nilai bilangan bulat yang tidak ditandatangani 8-bit yang ditentukan ke bilangan bulat bertanda tangan 64-bit yang setara.

public static long ToInt64 (byte value);

Parameter

value
Byte

Bilangan bulat 8-bit yang tidak ditandatangani untuk dikonversi.

Mengembalikan

Bilangan bulat bertanda tangan 64-bit yang setara dengan value.

Contoh

Contoh berikut mengonversi setiap elemen dalam array byte menjadi bilangan bulat panjang.

byte[] bytes = { Byte.MinValue, 14, 122, Byte.MaxValue};
long result;

foreach (byte byteValue in bytes)
{
   result = Convert.ToInt64(byteValue);
   Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.",
                     byteValue.GetType().Name, byteValue,
                     result.GetType().Name, result);
}
// The example displays the following output:
//       Converted the Byte value 0 to the Int64 value 0.
//       Converted the Byte value 14 to the Int64 value 14.
//       Converted the Byte value 122 to the Int64 value 122.
//       Converted the Byte value 255 to the Int64 value 255.

Berlaku untuk

.NET 9 dan versi lainnya
Produk Versi
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToInt64(Char)

Sumber:
Convert.cs
Sumber:
Convert.cs
Sumber:
Convert.cs

Mengonversi nilai karakter Unicode yang ditentukan menjadi bilangan bulat bertanda tangan 64-bit yang setara.

public static long ToInt64 (char value);

Parameter

value
Char

Karakter Unicode yang akan dikonversi.

Mengembalikan

Bilangan bulat bertanda tangan 64-bit yang setara dengan value.

Contoh

Contoh berikut mengonversi setiap elemen dalam Char array menjadi bilangan bulat panjang.

char[] chars = { 'a', 'z', '\u0007', '\u03FF',
                 '\u7FFF', '\uFFFE' };
long result;

foreach (char ch in chars)
{
   result = Convert.ToInt64(ch);
   Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.",
                     ch.GetType().Name, ch,
                     result.GetType().Name, result);
}
// The example displays the following output:
//       Converted the Char value 'a' to the Int64 value 97.
//       Converted the Char value 'z' to the Int64 value 122.
//       Converted the Char value '' to the Int64 value 7.
//       Converted the Char value 'Ͽ' to the Int64 value 1023.
//       Converted the Char value '翿' to the Int64 value 32767.
//       Converted the Char value '' to the Int64 value 65534.

Berlaku untuk

.NET 9 dan versi lainnya
Produk Versi
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToInt64(DateTime)

Sumber:
Convert.cs
Sumber:
Convert.cs
Sumber:
Convert.cs

Memanggil metode ini selalu melempar InvalidCastException.

public static long ToInt64 (DateTime value);

Parameter

value
DateTime

Nilai tanggal dan waktu untuk dikonversi.

Mengembalikan

Konversi ini tidak didukung. Tidak ada nilai yang dikembalikan.

Pengecualian

Konversi ini tidak didukung.

Berlaku untuk

.NET 9 dan versi lainnya
Produk Versi
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

ToInt64(Boolean)

Sumber:
Convert.cs
Sumber:
Convert.cs
Sumber:
Convert.cs

Mengonversi nilai Boolean yang ditentukan ke bilangan bulat bertanda tangan 64-bit yang setara.

public static long ToInt64 (bool value);

Parameter

value
Boolean

Nilai Boolean yang akan dikonversi.

Mengembalikan

Angka 1 jika value adalah true; jika tidak, 0.

Contoh

Contoh berikut mengonversi Boolean nilai true dan false ke bilangan bulat panjang.

bool falseFlag = false;
bool trueFlag = true;

Console.WriteLine("{0} converts to {1}.", falseFlag,
                  Convert.ToInt64(falseFlag));
Console.WriteLine("{0} converts to {1}.", trueFlag,
                  Convert.ToInt64(trueFlag));
// The example displays the following output:
//       False converts to 0.
//       True converts to 1.

Berlaku untuk

.NET 9 dan versi lainnya
Produk Versi
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToInt64(Double)

Sumber:
Convert.cs
Sumber:
Convert.cs
Sumber:
Convert.cs

Mengonversi nilai angka floating-point presisi ganda yang ditentukan menjadi bilangan bulat bertanda tangan 64-bit yang setara.

public static long ToInt64 (double value);

Parameter

value
Double

Angka floating-point presisi ganda untuk dikonversi.

Mengembalikan

value, dibulatkan ke bilangan bulat bertanda 64-bit terdekat. Jika value setengah jalan antara dua bilangan bujur, angka genap dikembalikan; yaitu, 4,5 dikonversi menjadi 4, dan 5,5 dikonversi menjadi 6.

Pengecualian

value lebih besar dari Int64.MaxValue atau kurang dari Int64.MinValue.

Contoh

Contoh berikut mencoba mengonversi setiap elemen dalam array nilai menjadi bilangan Double bulat panjang.

double[] values= { Double.MinValue, -1.38e10, -1023.299, -12.98,
                   0, 9.113e-16, 103.919, 17834.191, Double.MaxValue };
long result;

foreach (double value in values)
{
   try {
      result = Convert.ToInt64(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 Int64 type.", value);
   }
}
//    -1.7976931348623157E+308 is outside the range of the Int64 type.
//    Converted the Double value '-13800000000' to the Int64 value -13800000000.
//    Converted the Double value '-1023.299' to the Int64 value -1023.
//    Converted the Double value '-12.98' to the Int64 value -13.
//    Converted the Double value '0' to the Int64 value 0.
//    Converted the Double value '9.113E-16' to the Int64 value 0.
//    Converted the Double value '103.919' to the Int64 value 104.
//    Converted the Double value '17834.191' to the Int64 value 17834.
//    1.7976931348623157E+308 is outside the range of the Int64 type.

Lihat juga

Berlaku untuk

.NET 9 dan versi lainnya
Produk Versi
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToInt64(Int16)

Sumber:
Convert.cs
Sumber:
Convert.cs
Sumber:
Convert.cs

Mengonversi nilai bilangan bulat bertanda tangan 16-bit yang ditentukan menjadi bilangan bulat bertanda tangan 64-bit yang setara.

public static long ToInt64 (short value);

Parameter

value
Int16

Bilangan bulat bertanda tangan 16-bit untuk dikonversi.

Mengembalikan

Bilangan bulat bertanda tangan 64-bit yang setara dengan value.

Contoh

Contoh berikut mengonversi setiap elemen dalam array bilangan bulat 16-bit menjadi bilangan bulat panjang.

short[] numbers= { Int16.MinValue, -1, 0, 121, 340, Int16.MaxValue };
long result;

foreach (short number in numbers)
{
   result = Convert.ToInt64(number);
   Console.WriteLine("Converted the {0} value {1} to a {2} value {3}.",
                        number.GetType().Name, number,
                        result.GetType().Name, result);
}
// The example displays the following output:
//    Converted the Int16 value -32768 to a Int32 value -32768.
//    Converted the Int16 value -1 to a Int32 value -1.
//    Converted the Int16 value 0 to a Int32 value 0.
//    Converted the Int16 value 121 to a Int32 value 121.
//    Converted the Int16 value 340 to a Int32 value 340.
//    Converted the Int16 value 32767 to a Int32 value 32767.

Berlaku untuk

.NET 9 dan versi lainnya
Produk Versi
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToInt64(Int32)

Sumber:
Convert.cs
Sumber:
Convert.cs
Sumber:
Convert.cs

Mengonversi nilai bilangan bulat bertanda tangan 32-bit yang ditentukan menjadi bilangan bulat bertanda tangan 64-bit yang setara.

public static long ToInt64 (int value);

Parameter

value
Int32

Bilangan bulat bertanda tangan 32-bit untuk dikonversi.

Mengembalikan

Bilangan bulat bertanda tangan 64-bit yang setara dengan value.

Berlaku untuk

.NET 9 dan versi lainnya
Produk Versi
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToInt64(Decimal)

Sumber:
Convert.cs
Sumber:
Convert.cs
Sumber:
Convert.cs

Mengonversi nilai angka desimal yang ditentukan menjadi bilangan bulat bertanda tangan 64-bit yang setara.

public static long ToInt64 (decimal value);

Parameter

value
Decimal

Angka desimal yang akan dikonversi.

Mengembalikan

value, dibulatkan ke bilangan bulat bertanda 64-bit terdekat. Jika value setengah jalan antara dua bilangan bujur, angka genap dikembalikan; yaitu, 4,5 dikonversi menjadi 4, dan 5,5 dikonversi menjadi 6.

Pengecualian

value lebih besar dari Int64.MaxValue atau kurang dari Int64.MinValue.

Contoh

Contoh berikut mencoba mengonversi setiap elemen dalam array nilai menjadi bilangan Decimal bulat panjang.

decimal[] values= { Decimal.MinValue, -1034.23m, -12m, 0m, 147m,
                    199.55m, 9214.16m, Decimal.MaxValue };
long result;

foreach (decimal value in values)
{
   try {
      result = Convert.ToInt64(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 Int64 type.",
                        value);
   }
}
// The example displays the following output:
//    -79228162514264337593543950335 is outside the range of the Int64 type.
//    Converted the Decimal value '-1034.23' to the Int64 value -1034.
//    Converted the Decimal value '-12' to the Int64 value -12.
//    Converted the Decimal value '0' to the Int64 value 0.
//    Converted the Decimal value '147' to the Int64 value 147.
//    Converted the Decimal value '199.55' to the Int64 value 200.
//    Converted the Decimal value '9214.16' to the Int64 value 9214.
//    79228162514264337593543950335 is outside the range of the Int64 type.

Lihat juga

Berlaku untuk

.NET 9 dan versi lainnya
Produk Versi
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0