Decimal.Round Méthode

Définition

Arrondit une valeur à l'entier le plus proche ou au nombre spécifié de décimales.

Surcharges

Round(Decimal, Int32, MidpointRounding)

Arrondit une valeur décimale à la précision spécifiée à l’aide de la stratégie d’arrondi spécifiée.

Round(Decimal, MidpointRounding)

Arrondit une valeur décimale à un entier à l’aide de la stratégie d’arrondi spécifiée.

Round(Decimal, Int32)

Arrondit une valeur Decimal au nombre de décimales spécifié.

Round(Decimal)

Arrondit une valeur décimale à l'entier le plus proche.

Round(Decimal, Int32, MidpointRounding)

Source:
Decimal.cs
Source:
Decimal.cs
Source:
Decimal.cs

Arrondit une valeur décimale à la précision spécifiée à l’aide de la stratégie d’arrondi spécifiée.

public:
 static System::Decimal Round(System::Decimal d, int decimals, MidpointRounding mode);
public:
 static System::Decimal Round(System::Decimal d, int decimals, MidpointRounding mode) = System::Numerics::IFloatingPoint<System::Decimal>::Round;
public static decimal Round (decimal d, int decimals, MidpointRounding mode);
static member Round : decimal * int * MidpointRounding -> decimal
Public Shared Function Round (d As Decimal, decimals As Integer, mode As MidpointRounding) As Decimal

Paramètres

d
Decimal

Nombre décimal à arrondir.

decimals
Int32

Nombre de décimales significatives (précision) de la valeur de retour.

mode
MidpointRounding

Une des valeurs d’énumération qui spécifie la stratégie d’arrondi à utiliser.

Retours

Nombre arrondi d à à l’aide de la mode stratégie d’arrondi et avec une précision de decimals. Si la précision de d est inférieure à decimals, d est retourné sans modification.

Implémente

Exceptions

decimals est inférieur à 0 ou supérieur à 28.

mode n’est pas une valeur MidpointRounding.

Le résultat se situe hors de la plage d’un objet Decimal.

Exemples

L’exemple suivant montre comment utiliser la Round(Decimal, Int32, MidpointRounding) méthode avec l’énumération MidpointRounding .

decimal result;

// Round a positive value using different strategies.
// The precision of the result is 1 decimal place.

result = Math.Round(3.45m, 1, MidpointRounding.ToEven);
Console.WriteLine($"{result} = Math.Round({3.45m}, 1, MidpointRounding.ToEven)");
result = Math.Round(3.45m, 1, MidpointRounding.AwayFromZero);
Console.WriteLine($"{result} = Math.Round({3.45m}, 1, MidpointRounding.AwayFromZero)");
result = Math.Round(3.47m, 1, MidpointRounding.ToZero);
Console.WriteLine($"{result} = Math.Round({3.47m}, 1, MidpointRounding.ToZero)\n");

// Round a negative value using different strategies.
// The precision of the result is 1 decimal place.

result = Math.Round(-3.45m, 1, MidpointRounding.ToEven);
Console.WriteLine($"{result} = Math.Round({-3.45m}, 1, MidpointRounding.ToEven)");
result = Math.Round(-3.45m, 1, MidpointRounding.AwayFromZero);
Console.WriteLine($"{result} = Math.Round({-3.45m}, 1, MidpointRounding.AwayFromZero)");
result = Math.Round(-3.47m, 1, MidpointRounding.ToZero);
Console.WriteLine($"{result} = Math.Round({-3.47m}, 1, MidpointRounding.ToZero)\n");

/*
This code example produces the following results:

3.4 = Math.Round(3.45, 1, MidpointRounding.ToEven)
3.5 = Math.Round(3.45, 1, MidpointRounding.AwayFromZero)
3.4 = Math.Round(3.47, 1, MidpointRounding.ToZero)

-3.4 = Math.Round(-3.45, 1, MidpointRounding.ToEven)
-3.5 = Math.Round(-3.45, 1, MidpointRounding.AwayFromZero)
-3.4 = Math.Round(-3.47, 1, MidpointRounding.ToZero)
*/
// Round a positive value using different strategies.
// The precision of the result is 1 decimal place.

let result = Math.Round(3.45m, 1, MidpointRounding.ToEven)
printfn $"{result} = Math.Round({3.45m}, 1, MidpointRounding.ToEven)"
let result = Math.Round(3.45m, 1, MidpointRounding.AwayFromZero)
printfn $"{result} = Math.Round({3.45m}, 1, MidpointRounding.AwayFromZero)"
let result = Math.Round(3.47m, 1, MidpointRounding.ToZero)
printfn $"{result} = Math.Round({3.47m}, 1, MidpointRounding.ToZero)\n"

// Round a negative value using different strategies.
// The precision of the result is 1 decimal place.

let result = Math.Round(-3.45m, 1, MidpointRounding.ToEven)
printfn $"{result} = Math.Round({-3.45m}, 1, MidpointRounding.ToEven)"
let result = Math.Round(-3.45m, 1, MidpointRounding.AwayFromZero)
printfn $"{result} = Math.Round({-3.45m}, 1, MidpointRounding.AwayFromZero)"
let result = Math.Round(-3.47m, 1, MidpointRounding.ToZero)
printfn $"{result} = Math.Round({-3.47m}, 1, MidpointRounding.ToZero)\n"

// This code example produces the following results:

// 3.4 = Math.Round(3.45, 1, MidpointRounding.ToEven)
// 3.5 = Math.Round(3.45, 1, MidpointRounding.AwayFromZero)
// 3.4 = Math.Round(3.47, 1, MidpointRounding.ToZero)

// -3.4 = Math.Round(-3.45, 1, MidpointRounding.ToEven)
// -3.5 = Math.Round(-3.45, 1, MidpointRounding.AwayFromZero)
// -3.4 = Math.Round(-3.47, 1, MidpointRounding.ToZero)
Dim result As Decimal = 0D
Dim posValue As Decimal = 3.45D
Dim negValue As Decimal = -3.45D

' Round a positive value using different strategies.
' The precision of the result is 1 decimal place.
result = Math.Round(posValue, 1, MidpointRounding.ToEven)
Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.ToEven)",
                   result, posValue)
result = Math.Round(posValue, 1, MidpointRounding.AwayFromZero)
Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.AwayFromZero)",
                   result, posValue)
result = Math.Round(posValue, 1, MidpointRounding.ToZero)
Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.ToZero)",
                   result, posValue)
Console.WriteLine()

' Round a negative value using different strategies.
' The precision of the result is 1 decimal place.
result = Math.Round(negValue, 1, MidpointRounding.ToEven)
Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.ToEven)",
                    result, negValue)
result = Math.Round(negValue, 1, MidpointRounding.AwayFromZero)
Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.AwayFromZero)",
                   result, negValue)
result = Math.Round(negValue, 1, MidpointRounding.ToZero)
Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.ToZero)",
                   result, negValue)
Console.WriteLine()

'This code example produces the following results:
'
'        3.4 = Math.Round(3.45, 1, MidpointRounding.ToEven)
'        3.5 = Math.Round(3.45, 1, MidpointRounding.AwayFromZero)
'        3.4 = Math.Round(3.45, 1, MidpointRounding.ToZero)
'
'        -3.4 = Math.Round(-3.45, 1, MidpointRounding.ToEven)
'        -3.5 = Math.Round(-3.45, 1, MidpointRounding.AwayFromZero)
'        -3.4 = Math.Round(-3.45, 1, MidpointRounding.ToZero)
'

Remarques

Le decimals paramètre spécifie le nombre de décimales significatives dans la valeur de retour et les plages comprises entre 0 et 28. Si decimals est zéro, un entier est retourné.

Si vous spécifiez ToEven ou AwayFromZero pour le paramètre, ces stratégies d’arrondi sont appliquées uniquement pour les valeurs médianes, c’est-à-dire les valeurs dont le mode chiffre le moins significatif est 5.

Voir aussi

S’applique à

Round(Decimal, MidpointRounding)

Source:
Decimal.cs
Source:
Decimal.cs
Source:
Decimal.cs

Arrondit une valeur décimale à un entier à l’aide de la stratégie d’arrondi spécifiée.

public:
 static System::Decimal Round(System::Decimal d, MidpointRounding mode);
public:
 static System::Decimal Round(System::Decimal d, MidpointRounding mode) = System::Numerics::IFloatingPoint<System::Decimal>::Round;
public static decimal Round (decimal d, MidpointRounding mode);
static member Round : decimal * MidpointRounding -> decimal
Public Shared Function Round (d As Decimal, mode As MidpointRounding) As Decimal

Paramètres

d
Decimal

Nombre décimal à arrondir.

mode
MidpointRounding

Une des valeurs d’énumération qui spécifie la stratégie d’arrondi à utiliser.

Retours

Entier d arrondi à à l’aide de la mode stratégie d’arrondi.

Implémente

Exceptions

mode n’est pas une valeur MidpointRounding.

Le résultat se situe hors de la plage d’un objet Decimal.

Exemples

L’exemple suivant affiche les valeurs retournées par la Round(Decimal, MidpointRounding) méthode avec différents mode arguments.

Console.WriteLine($"{"Value",-10} {"Default",-10} {"ToEven",-10} {"AwayFromZero",-15} {"ToZero",-15}");
for (decimal value = 12.0m; value <= 13.0m; value += 0.1m)
    Console.WriteLine($"{value,-10} {Math.Round(value),-10} " +
        $"{Math.Round(value, MidpointRounding.ToEven),-10} " +
        $"{Math.Round(value, MidpointRounding.AwayFromZero),-15} " +
        $"{Math.Round(value, MidpointRounding.ToZero),-15}");

// The example displays the following output:
//       Value      Default    ToEven     AwayFromZero    ToZero
//       12.0       12         12         12              12
//       12.1       12         12         12              12
//       12.2       12         12         12              12
//       12.3       12         12         12              12
//       12.4       12         12         12              12
//       12.5       12         12         13              12
//       12.6       13         13         13              12
//       12.7       13         13         13              12
//       12.8       13         13         13              12
//       12.9       13         13         13              12
//       13.0       13         13         13              13
printfn $"""{"Value",-10} {"Default",-10} {"ToEven",-10} {"AwayFromZero",-15} {"ToZero",-15}"""
for value in 12m .. 0.1m .. 13m do
    printfn "%-10O %-10O %-10O %-15O %-15O" 
        value
        (Math.Round value)
        (Math.Round(value, MidpointRounding.ToEven))
        (Math.Round(value, MidpointRounding.AwayFromZero))
        (Math.Round(value, MidpointRounding.ToZero))

// The example displays the following output:
//       Value      Default    ToEven     AwayFromZero    ToZero
//       12.0       12         12         12              12
//       12.1       12         12         12              12
//       12.2       12         12         12              12
//       12.3       12         12         12              12
//       12.4       12         12         12              12
//       12.5       12         12         13              12
//       12.6       13         13         13              12
//       12.7       13         13         13              12
//       12.8       13         13         13              12
//       12.9       13         13         13              12
//       13.0       13         13         13              13
Console.WriteLine("{0,-10} {1,-10} {2,-10} {3,-15} {4,-15}", "Value", "Default",
                "ToEven", "AwayFromZero", "ToZero")
For value As Decimal = 12D To 13D Step 0.1D
    Console.WriteLine("{0,-10} {1,-10} {2,-10} {3,-15} {4,-15}",
                   value, Math.Round(value),
                   Math.Round(value, MidpointRounding.ToEven),
                   Math.Round(value, MidpointRounding.AwayFromZero),
                   Math.Round(value, MidpointRounding.ToZero))
Next

' The example displays the following output:
'       Value      Default    ToEven     AwayFromZero     ToZero
'       12         12         12         12               12
'       12.1       12         12         12               12
'       12.2       12         12         12               12
'       12.3       12         12         12               12
'       12.4       12         12         12               12
'       12.5       12         12         13               12
'       12.6       13         13         13               12
'       12.7       13         13         13               12
'       12.8       13         13         13               12
'       12.9       13         13         13               12
'       13.0       13         13         13               13

Remarques

Si vous spécifiez ToEven ou AwayFromZero pour le paramètre, ces stratégies d’arrondi sont appliquées uniquement pour les valeurs médianes, c’est-à-dire les valeurs dont le mode chiffre le moins significatif est 5.

Voir aussi

S’applique à

Round(Decimal, Int32)

Source:
Decimal.cs
Source:
Decimal.cs
Source:
Decimal.cs

Arrondit une valeur Decimal au nombre de décimales spécifié.

public:
 static System::Decimal Round(System::Decimal d, int decimals);
public:
 static System::Decimal Round(System::Decimal d, int decimals) = System::Numerics::IFloatingPoint<System::Decimal>::Round;
public static decimal Round (decimal d, int decimals);
static member Round : decimal * int -> decimal
Public Shared Function Round (d As Decimal, decimals As Integer) As Decimal

Paramètres

d
Decimal

Nombre décimal à arrondir.

decimals
Int32

Valeur de 0 à 28 qui spécifie le nombre de décimales auquel arrondir le chiffre.

Retours

Nombre décimal équivalent à arrondi à ddecimals des décimales.

Implémente

Exceptions

decimals n’est pas une valeur comprise entre 0 et 28.

Exemples

L’exemple suivant arrondit plusieurs Decimal valeurs à un nombre spécifié de décimales à l’aide de la Round méthode .

using System;

class Example12
{
   public static void Main()
   {
      // Define a set of Decimal values.
      decimal[] values = { 1.45m, 1.55m, 123.456789m, 123.456789m,
                           123.456789m, -123.456m,
                           new Decimal(1230000000, 0, 0, true, 7 ),
                           new Decimal(1230000000, 0, 0, true, 7 ),
                           -9999999999.9999999999m,
                           -9999999999.9999999999m };
      // Define a set of integers to for decimals argument.
      int[] decimals = { 1, 1, 4, 6, 8, 0, 3, 11, 9, 10};

      Console.WriteLine("{0,26}{1,8}{2,26}",
                        "Argument", "Digits", "Result" );
      Console.WriteLine("{0,26}{1,8}{2,26}",
                        "--------", "------", "------" );
      for (int ctr = 0; ctr < values.Length; ctr++)
        Console.WriteLine("{0,26}{1,8}{2,26}",
                          values[ctr], decimals[ctr],
                          Decimal.Round(values[ctr], decimals[ctr]));
    }
}
// The example displays the following output:
//                   Argument  Digits                    Result
//                   --------  ------                    ------
//                       1.45       1                       1.4
//                       1.55       1                       1.6
//                 123.456789       4                  123.4568
//                 123.456789       6                123.456789
//                 123.456789       8                123.456789
//                   -123.456       0                      -123
//               -123.0000000       3                  -123.000
//               -123.0000000      11              -123.0000000
//     -9999999999.9999999999       9    -10000000000.000000000
//     -9999999999.9999999999      10    -9999999999.9999999999
open System

// Define a set of Decimal values.
let values = 
    [ 1.45m; 1.55m; 123.456789m; 123.456789m
      123.456789m; -123.456m
      Decimal(1230000000, 0, 0, true, 7uy)
      Decimal(1230000000, 0, 0, true, 7uy)
      -9999999999.9999999999m
      -9999999999.9999999999m ]

// Define a set of integers to for decimals argument.
let decimals = 
    [ 1; 1; 4; 6; 8; 0; 3; 11; 9; 10 ]

printfn $"""{"Argument",26}{"Digits",8}{"Result",26}"""
printfn $"""{"--------",26}{"------",8}{"------",26}"""

for i = 0 to values.Length - 1 do
    printfn $"{values[i],26}{decimals[i],8}{Decimal.Round(values[i], decimals[i]),26}"

// The example displays the following output:
//                   Argument  Digits                    Result
//                   --------  ------                    ------
//                       1.45       1                       1.4
//                       1.55       1                       1.6
//                 123.456789       4                  123.4568
//                 123.456789       6                123.456789
//                 123.456789       8                123.456789
//                   -123.456       0                      -123
//               -123.0000000       3                  -123.000
//               -123.0000000      11              -123.0000000
//     -9999999999.9999999999       9    -10000000000.000000000
//     -9999999999.9999999999      10    -9999999999.9999999999
Public Module Example
   Public Sub Main()
      ' Define a set of Decimal values.
      Dim values() As Decimal = { 1.45d, 1.55d, 123.456789d, 123.456789d, 
                                  123.456789d, -123.456d, 
                                  New Decimal(1230000000, 0, 0, true, 7 ),
                                  New Decimal(1230000000, 0, 0, true, 7 ), 
                                  -9999999999.9999999999d, 
                                  -9999999999.9999999999d }
      ' Define a set of integers to for decimals argument.
      Dim decimals() As Integer = { 1, 1, 4, 6, 8, 0, 3, 11, 9, 10}
      
      Console.WriteLine("{0,26}{1,8}{2,26}", 
                        "Argument", "Digits", "Result" )
      Console.WriteLine("{0,26}{1,8}{2,26}", 
                        "--------", "------", "------" )
      For ctr As Integer = 0 To values.Length - 1
        Console.WriteLine("{0,26}{1,8}{2,26}", 
                          values(ctr), decimals(ctr), 
                          Decimal.Round(values(ctr), decimals(ctr)))
      Next
   End Sub
End Module
' The example displays the following output:
'                   Argument  Digits                    Result
'                   --------  ------                    ------
'                       1.45       1                       1.4
'                       1.55       1                       1.6
'                 123.456789       4                  123.4568
'                 123.456789       6                123.456789
'                 123.456789       8                123.456789
'                   -123.456       0                      -123
'               -123.0000000       3                  -123.000
'               -123.0000000      11              -123.0000000
'     -9999999999.9999999999       9    -10000000000.000000000
'     -9999999999.9999999999      10    -9999999999.9999999999

Remarques

Cette méthode équivaut à appeler la Round(Decimal, Int32, MidpointRounding) méthode avec un mode argument de MidpointRounding.ToEven. Quand d est exactement à mi-chemin entre deux valeurs arrondies, le résultat est la valeur arrondie qui a un chiffre pair dans la position décimale à droite. Par exemple, lorsqu'elle est arrondie à deux décimales, la valeur 2,345 devient 2,34 et la valeur 2,355 devient 2,36. Ce processus est connu sous le nom d’arrondi vers pair, ou arrondi de banquier. Il réduit au minimum les erreurs d’arrondi qui résultent de l’arrondi constant d’une valeur médiane dans une seule direction.

Voir aussi

S’applique à

Round(Decimal)

Source:
Decimal.cs
Source:
Decimal.cs
Source:
Decimal.cs

Arrondit une valeur décimale à l'entier le plus proche.

public:
 static System::Decimal Round(System::Decimal d);
public:
 static System::Decimal Round(System::Decimal d) = System::Numerics::IFloatingPoint<System::Decimal>::Round;
public static decimal Round (decimal d);
static member Round : decimal -> decimal
Public Shared Function Round (d As Decimal) As Decimal

Paramètres

d
Decimal

Nombre décimal à arrondir.

Retours

Entier le plus proche du paramètre d. Si d se trouve à mi-chemin entre deux entiers, l'un pair et l'autre impair, le nombre pair est retourné.

Implémente

Exceptions

Le résultat se situe hors de la plage d’une valeur Decimal.

Exemples

L’exemple suivant arrondit une plage de Decimal valeurs comprises entre 100 et 102 à l’entier le plus proche. Étant donné que la méthode utilise l’arrondi du banquier, 100,5 tours à 100 et 101,5 tours à 102.

using System;

public class Example
{
   public static void Main()
   {
      for (decimal value = 100m; value <= 102m; value += .1m)
         Console.WriteLine("{0} --> {1}", value, Decimal.Round(value));
   }
}
// The example displays the following output:
//     100 --> 100
//     100.1 --> 100
//     100.2 --> 100
//     100.3 --> 100
//     100.4 --> 100
//     100.5 --> 100
//     100.6 --> 101
//     100.7 --> 101
//     100.8 --> 101
//     100.9 --> 101
//     101.0 --> 101
//     101.1 --> 101
//     101.2 --> 101
//     101.3 --> 101
//     101.4 --> 101
//     101.5 --> 102
//     101.6 --> 102
//     101.7 --> 102
//     101.8 --> 102
//     101.9 --> 102
//     102.0 --> 102
open System

for value in 100m .. 0.1m .. 102m do
    printfn $"{value} --> {Decimal.Round value}"

// The example displays the following output:
//     100 --> 100
//     100.1 --> 100
//     100.2 --> 100
//     100.3 --> 100
//     100.4 --> 100
//     100.5 --> 100
//     100.6 --> 101
//     100.7 --> 101
//     100.8 --> 101
//     100.9 --> 101
//     101.0 --> 101
//     101.1 --> 101
//     101.2 --> 101
//     101.3 --> 101
//     101.4 --> 101
//     101.5 --> 102
//     101.6 --> 102
//     101.7 --> 102
//     101.8 --> 102
//     101.9 --> 102
//     102.0 --> 102
Module Example
   Public Sub Main()
      For value As Decimal = 100d To 102d Step .1d
         Console.WriteLine("{0} --> {1}", value, Decimal.Round(value))
      Next
   End Sub
End Module
' The example displays the following output:
'     100 --> 100
'     100.1 --> 100
'     100.2 --> 100
'     100.3 --> 100
'     100.4 --> 100
'     100.5 --> 100
'     100.6 --> 101
'     100.7 --> 101
'     100.8 --> 101
'     100.9 --> 101
'     101.0 --> 101
'     101.1 --> 101
'     101.2 --> 101
'     101.3 --> 101
'     101.4 --> 101
'     101.5 --> 102
'     101.6 --> 102
'     101.7 --> 102
'     101.8 --> 102
'     101.9 --> 102
'     102.0 --> 102

Remarques

Le comportement de cette méthode suit la norme IEEE 754, section 4. Ce type d’arrondi est parfois appelé demi-ronde à pair ou arrondi de banquier. Il réduit au minimum les erreurs d’arrondi qui résultent de l’arrondi constant d’une valeur médiane dans une seule direction. Cela équivaut à appeler la Round(Decimal, MidpointRounding) méthode avec un mode argument de MidpointRounding.ToEven.

Voir aussi

S’applique à