本文整理汇总了C#中System.Numerics.BigInteger.Explicit操作符的典型用法代码示例。如果您正苦于以下问题:C# BigInteger.Explicit操作符的具体用法?C# BigInteger.Explicit怎么用?C# BigInteger.Explicit使用的例子?那么恭喜您, 这里精选的操作符代码示例或许可以为您提供帮助。您也可以进一步了解该操作符所在类System.Numerics.BigInteger
的用法示例。
在下文中一共展示了BigInteger.Explicit操作符的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1:
decimal[] decimals = { Decimal.MinValue, -15632.991m, 9029321.12m,
Decimal.MaxValue };
BigInteger number;
Console.WriteLine("{0,35} {1,35}\n", "Decimal", "BigInteger");
foreach (decimal value in decimals)
{
number = (BigInteger) value;
Console.WriteLine("{0,35} {1,35}", value, number);
}
输出:
Decimal BigInteger -79228162514264337593543950335 -79228162514264337593543950335 -15632.991 -15632 9029321.12 9029321 79228162514264337593543950335 79228162514264337593543950335
示例2: catch
double[] doubles = { Double.MinValue, -1.430955172e03, 2.410970032e05,
Double.MaxValue, Double.PositiveInfinity,
Double.NegativeInfinity, Double.NaN };
BigInteger number;
Console.WriteLine("{0,37} {1,37}\n", "Double", "BigInteger");
foreach (double value in doubles)
{
try {
number = (BigInteger) value;
Console.WriteLine("{0,37} {1,37}", value, number);
}
catch (OverflowException) {
Console.WriteLine("{0,37} {1,37}", value, "OverflowException");
}
}
输出:
Double BigInteger -1.79769313486232E+308 -1.7976931348623157081452742373E+308 -1430.955172 -1430 241097.0032 241097 1.79769313486232E+308 1.7976931348623157081452742373E+308 Infinity OverflowException -Infinity OverflowException NaN OverflowException
示例3:
// BigInteger to Byte conversion.
BigInteger goodByte = BigInteger.One;
BigInteger badByte = 256;
byte byteFromBigInteger;
// Successful conversion using cast operator.
byteFromBigInteger = (byte) goodByte;
Console.WriteLine(byteFromBigInteger);
// Handle conversion that should result in overflow.
try
{
byteFromBigInteger = (byte) badByte;
Console.WriteLine(byteFromBigInteger);
}
catch (OverflowException e)
{
Console.WriteLine("Unable to convert {0}:\n {1}",
badByte, e.Message);
}
Console.WriteLine();
示例4:
// BigInteger to Decimal conversion.
BigInteger goodDecimal = 761652543;
BigInteger badDecimal = (BigInteger) Decimal.MaxValue;
badDecimal += BigInteger.One;
Decimal decimalFromBigInteger;
// Successful conversion using cast operator.
decimalFromBigInteger = (decimal) goodDecimal;
Console.WriteLine(decimalFromBigInteger);
// Handle conversion that should result in overflow.
try
{
decimalFromBigInteger = (decimal) badDecimal;
Console.WriteLine(decimalFromBigInteger);
}
catch (OverflowException e)
{
Console.WriteLine("Unable to convert {0}:\n {1}",
badDecimal, e.Message);
}
Console.WriteLine();
示例5:
// BigInteger to Double conversion.
BigInteger goodDouble = (BigInteger) 102.43e22;
BigInteger badDouble = (BigInteger) Double.MaxValue;
badDouble = badDouble * 2;
double doubleFromBigInteger;
// successful conversion using cast operator.
doubleFromBigInteger = (double) goodDouble;
Console.WriteLine(doubleFromBigInteger);
// Convert an out-of-bounds BigInteger value to a Double.
doubleFromBigInteger = (double) badDouble;
Console.WriteLine(doubleFromBigInteger);
示例6:
// Increase a BigInteger so it exceeds Double.MaxValue.
BigInteger number1 = (BigInteger) Double.MaxValue;
BigInteger number2 = number1;
number2 = number2 + (BigInteger) 9.999e291;
// Compare the BigInteger values for equality.
Console.WriteLine("BigIntegers equal: {0}", number2.Equals(number1));
// Convert the BigInteger to a Double.
double dbl = (double) number2;
// Display the two values.
Console.WriteLine("BigInteger: {0}", number2);
Console.WriteLine("Double: {0}", dbl);
输出:
BigIntegers equal: False BigInteger: 1.7976931348623158081352742373E+308 Double: 1.79769313486232E+308
示例7:
// BigInteger to Int16 conversion.
BigInteger goodShort = 20000;
BigInteger badShort = 33000;
short shortFromBigInteger;
// Successful conversion using cast operator.
shortFromBigInteger = (short) goodShort;
Console.WriteLine(shortFromBigInteger);
// Handle conversion that should result in overflow.
try
{
shortFromBigInteger = (short) badShort;
Console.WriteLine(shortFromBigInteger);
}
catch (OverflowException e)
{
Console.WriteLine("Unable to convert {0}:\n {1}",
badShort, e.Message);
}
Console.WriteLine();
示例8:
// BigInteger to Int32 conversion.
BigInteger goodInteger = 200000;
BigInteger badInteger = 65000000000;
int integerFromBigInteger;
// Successful conversion using cast operator.
integerFromBigInteger = (int) goodInteger;
Console.WriteLine(integerFromBigInteger);
// Handle conversion that should result in overflow.
try
{
integerFromBigInteger = (int) badInteger;
Console.WriteLine(integerFromBigInteger);
}
catch (OverflowException e)
{
Console.WriteLine("Unable to convert {0}:\n {1}",
badInteger, e.Message);
}
Console.WriteLine();
示例9:
// BigInteger to Int64 conversion.
BigInteger goodLong = 2000000000;
BigInteger badLong = BigInteger.Pow(goodLong, 3);
long longFromBigInteger;
// Successful conversion using cast operator.
longFromBigInteger = (long) goodLong;
Console.WriteLine(longFromBigInteger);
// Handle conversion that should result in overflow.
try
{
longFromBigInteger = (long) badLong;
Console.WriteLine(longFromBigInteger);
}
catch (OverflowException e)
{
Console.WriteLine("Unable to convert {0}:\n {1}",
badLong, e.Message);
}
Console.WriteLine();
示例10:
// BigInteger to SByte conversion.
BigInteger goodSByte = BigInteger.MinusOne;
BigInteger badSByte = -130;
sbyte sByteFromBigInteger;
// Successful conversion using cast operator.
sByteFromBigInteger = (sbyte) goodSByte;
Console.WriteLine(sByteFromBigInteger);
// Handle conversion that should result in overflow.
try
{
sByteFromBigInteger = (sbyte) badSByte;
Console.WriteLine(sByteFromBigInteger);
}
catch (OverflowException e)
{
Console.WriteLine("Unable to convert {0}:\n {1}",
badSByte, e.Message);
}
Console.WriteLine();
示例11:
// BigInteger to Single conversion.
BigInteger goodSingle = (BigInteger) 102.43e22F;
BigInteger badSingle = (BigInteger) float.MaxValue;
badSingle = badSingle * 2;
float singleFromBigInteger;
// Successful conversion using cast operator.
singleFromBigInteger = (float) goodSingle;
Console.WriteLine(singleFromBigInteger);
// Convert an out-of-bounds BigInteger value to a Single.
singleFromBigInteger = (float) badSingle;
Console.WriteLine(singleFromBigInteger);
示例12:
// Increase a BigInteger so it exceeds Single.MaxValue.
BigInteger number1 = (BigInteger) Single.MaxValue;
BigInteger number2 = number1;
number2 = number2 + (BigInteger) 9.999e30;
// Compare the BigInteger values for equality.
Console.WriteLine("BigIntegers equal: {0}", number2.Equals(number1));
// Convert the BigInteger to a Single.
float sng = (float) number2;
// Display the two values.
Console.WriteLine("BigInteger: {0}", number2);
Console.WriteLine("Single: {0}", sng);
输出:
BigIntegers equal: False BigInteger: 3.4028235663752885981170396038E+38 Single: 3.402823E+38
示例13:
// BigInteger to UInt16 conversion.
BigInteger goodUShort = 20000;
BigInteger badUShort = 66000;
ushort uShortFromBigInteger;
// Successful conversion using cast operator.
uShortFromBigInteger = (ushort) goodUShort;
Console.WriteLine(uShortFromBigInteger);
// Handle conversion that should result in overflow.
try
{
uShortFromBigInteger = (ushort) badUShort;
Console.WriteLine(uShortFromBigInteger);
}
catch (OverflowException e)
{
Console.WriteLine("Unable to convert {0}:\n {1}",
badUShort, e.Message);
}
Console.WriteLine();
示例14:
// BigInteger to UInt32 conversion.
BigInteger goodUInteger = 200000;
BigInteger badUInteger = 65000000000;
uint uIntegerFromBigInteger;
// Successful conversion using cast operator.
uIntegerFromBigInteger = (uint) goodInteger;
Console.WriteLine(uIntegerFromBigInteger);
// Handle conversion that should result in overflow.
try
{
uIntegerFromBigInteger = (uint) badUInteger;
Console.WriteLine(uIntegerFromBigInteger);
}
catch (OverflowException e)
{
Console.WriteLine("Unable to convert {0}:\n {1}",
badUInteger, e.Message);
}
Console.WriteLine();
示例15:
// BigInteger to UInt64 conversion.
BigInteger goodULong = 2000000000;
BigInteger badULong = BigInteger.Pow(goodULong, 3);
ulong uLongFromBigInteger;
// Successful conversion using cast operator.
uLongFromBigInteger = (ulong) goodULong;
Console.WriteLine(uLongFromBigInteger);
// Handle conversion that should result in overflow.
try
{
uLongFromBigInteger = (ulong) badULong;
Console.WriteLine(uLongFromBigInteger);
}
catch (OverflowException e)
{
Console.WriteLine("Unable to convert {0}:\n {1}",
badULong, e.Message);
}
Console.WriteLine();