本文整理汇总了C#中System.Int64结构体的典型用法代码示例。如果您正苦于以下问题:C# Int64结构体的具体用法?C# Int64怎么用?C# Int64使用的例子?那么恭喜您, 这里精选的结构体代码示例或许可以为您提供帮助。
Int64结构体属于System命名空间,在下文中一共展示了Int64结构体的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1:
sbyte value1 = 124;
short value2 = 1618;
int value3 = Int32.MaxValue;
long number1 = value1;
long number2 = value2;
long number3 = value3;
示例2: catch
ulong ulNumber = 163245617943825;
try {
long number1 = (long) ulNumber;
Console.WriteLine(number1);
}
catch (OverflowException) {
Console.WriteLine("{0} is out of range of an Int64.", ulNumber);
}
double dbl2 = 35901.997;
try {
long number2 = (long) dbl2;
Console.WriteLine(number2);
}
catch (OverflowException) {
Console.WriteLine("{0} is out of range of an Int64.", dbl2);
}
BigInteger bigNumber = (BigInteger) 1.63201978555e30;
try {
long number3 = (long) bigNumber;
Console.WriteLine(number3);
}
catch (OverflowException) {
Console.WriteLine("{0} is out of range of an Int64.", bigNumber);
}
输出:
163245617943825 35902 1,632,019,785,549,999,969,612,091,883,520 is out of range of an Int64.
示例3: foreach
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);
}
}
输出:
-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.
示例4: catch
string string1 = "244681903147";
try {
long number1 = Int64.Parse(string1);
Console.WriteLine(number1);
}
catch (OverflowException) {
Console.WriteLine("'{0}' is out of range of a 64-bit integer.", string1);
}
catch (FormatException) {
Console.WriteLine("The format of '{0}' is invalid.", string1);
}
string string2 = "F9A3CFF0A";
try {
long number2 = Int64.Parse(string2,
System.Globalization.NumberStyles.HexNumber);
Console.WriteLine(number2);
}
catch (OverflowException) {
Console.WriteLine("'{0}' is out of range of a 64-bit integer.", string2);
}
catch (FormatException) {
Console.WriteLine("The format of '{0}' is invalid.", string2);
}
输出:
244681903147 67012198154
示例5:
long[] numbers = { -1403, 0, 169, 1483104 };
foreach (var 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,8:D3}", number);
// Display value with 1 decimal digit.
Console.Write("{0,13:N1}", number);
// Display value as hexadecimal.
Console.Write("{0,18:X2}", number);
// Display value with eight hexadecimal digits.
Console.WriteLine("{0,18:X8}", number);
}
输出:
-1403 --> -1403 -1,403.0 FFFFFFFFFFFFFA85 FFFFFFFFFFFFFA85 0 --> 000 0.0 00 00000000 169 --> 169 169.0 A9 000000A9 1483104 --> 1483104 1,483,104.0 16A160 0016A160
示例6:
long[] numbers = { -146, 11043, 2781913 };
foreach (var number in numbers) {
Console.WriteLine("{0} (Base 10):", number);
Console.WriteLine(" Binary: {0}", Convert.ToString(number, 2));
Console.WriteLine(" Octal: {0}", Convert.ToString(number, 8));
Console.WriteLine(" Hex: {0}\n", Convert.ToString(number, 16));
}
输出:
-146 (Base 10): Binary: 1111111111111111111111111111111111111111111111111111111101101110 Octal: 1777777777777777777556 Hex: ffffffffffffff6e 11043 (Base 10): Binary: 10101100100011 Octal: 25443 Hex: 2b23 2781913 (Base 10): Binary: 1010100111001011011001 Octal: 12471331 Hex: 2a72d9