EN
Ce contenu n’est pas disponible dans votre langue. Voici la version anglaise.
Ce sujet n'a pas encore été évalué - Évaluez ce sujet

# Decimal Explicit Conversion (Decimal to Int32)

February 14, 2014

Converts a Decimal to a 32-bit signed integer.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
```public static explicit operator int (
decimal value
)
```

#### Parameters

value
Type: System.Decimal
A Decimal to convert.

#### Return Value

Type: System.Int32
A 32-bit signed integer that represents the converted Decimal.
ExceptionCondition
OverflowException

value is less than Int32.MinValue or greater than Int32.MaxValue.

This operator supports the explicit conversion of a Decimal to a Int32. The syntax for such explicit conversions is language-dependent, and individual language compilers can provide different implementations and return different results. The example illustrates the different return values when you explicitly convert a Decimal value to an Int32 value by using C# and Visual Basic. To perform a conversion that is independent of language, you can call the ToInt32 or the Convert.ToInt32(Decimal) method.

The following code example converts Decimal numbers to Int32 values using the explicit Decimal to Int32 conversion.

```
// Example of the explicit conversions from decimal to int and
// decimal to uint.
using System;

class Example
{
const string formatter = "{0,17}{1,19}{2,19}";

// Get the exception type name; remove the namespace prefix.
public static string GetExceptionType(Exception ex)
{
string exceptionType = ex.GetType().ToString();
return exceptionType.Substring(
exceptionType.LastIndexOf('.') + 1);
}

// Convert the decimal argument; catch exceptions that are thrown.
public static void DecimalToU_Int32(System.Windows.Controls.TextBlock outputBlock, decimal argument)
{
object Int32Value;
object UInt32Value;

// Convert the argument to an int value.
try
{
Int32Value = (int)argument;
}
catch (Exception ex)
{
Int32Value = GetExceptionType(ex);
}

// Convert the argument to a uint value.
try
{
UInt32Value = (uint)argument;
}
catch (Exception ex)
{
UInt32Value = GetExceptionType(ex);
}

outputBlock.Text += String.Format(formatter, argument,
Int32Value, UInt32Value) + "\n";
}

public static void Demo(System.Windows.Controls.TextBlock outputBlock)
{
outputBlock.Text += String.Format(
"This example of the explicit conversions from decimal " +
"to int \nand decimal to uint generates the following " +
"output. It displays \nseveral converted decimal " +
"values.\n") + "\n";
outputBlock.Text += String.Format(formatter, "decimal argument",
"int/exception", "uint/exception") + "\n";
outputBlock.Text += String.Format(formatter, "----------------",
"-------------", "--------------") + "\n";

// Convert decimal values and display the results.
DecimalToU_Int32(outputBlock, 123M);
DecimalToU_Int32(outputBlock, new decimal(123000, 0, 0, false, 3));
DecimalToU_Int32(outputBlock, 123.999M);
DecimalToU_Int32(outputBlock, 4294967295.999M);
DecimalToU_Int32(outputBlock, 4294967296M);
DecimalToU_Int32(outputBlock, 2147483647.999M);
DecimalToU_Int32(outputBlock, 2147483648M);
DecimalToU_Int32(outputBlock, -0.999M);
DecimalToU_Int32(outputBlock, -1M);
DecimalToU_Int32(outputBlock, -2147483648.999M);
DecimalToU_Int32(outputBlock, -2147483649M);
}
}

/*
This example of the explicit conversions from decimal to int
and decimal to uint generates the following output. It displays
several converted decimal values.

decimal argument      int/exception     uint/exception
----------------      -------------     --------------
123                123                123
123.000                123                123
123.999                123                123
4294967295.999  OverflowException         4294967295
4294967296  OverflowException  OverflowException
2147483647.999         2147483647         2147483647
2147483648  OverflowException         2147483648
-0.999                  0                  0
-1                 -1  OverflowException
-2147483648.999        -2147483648  OverflowException
-2147483649  OverflowException  OverflowException
*/

```

#### Windows Phone OS

Supported in: 8.0, 7.1, 7.0

Windows Phone

Cela vous a-t-il été utile ?
(1500 caractères restants)
Merci pour vos suggestions.
Afficher:
© 2014 Microsoft. Tous droits réservés.