Decimal.Round Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Arrotonda un valore all'intero più vicino o al numero specificato di posizioni decimali.
Overload
| Nome | Descrizione |
|---|---|
| Round(Decimal, Int32, MidpointRounding) |
Arrotonda un valore decimale alla precisione specificata utilizzando la strategia di arrotondamento specificata. |
| Round(Decimal, MidpointRounding) |
Arrotonda un valore decimale a un numero intero utilizzando la strategia di arrotondamento specificata. |
| Round(Decimal) |
Arrotonda un valore decimale all'intero più vicino. |
| Round(Decimal, Int32) |
Arrotonda un Decimal valore a un numero specificato di posizioni decimali. |
Round(Decimal, Int32, MidpointRounding)
- Origine:
- Decimal.cs
- Origine:
- Decimal.cs
- Origine:
- Decimal.cs
- Origine:
- Decimal.cs
- Origine:
- Decimal.cs
Arrotonda un valore decimale alla precisione specificata utilizzando la strategia di arrotondamento specificata.
public:
static System::Decimal Round(System::Decimal d, int decimals, MidpointRounding mode) = System::Numerics::IFloatingPoint<System::Decimal>::Round;
public:
static System::Decimal Round(System::Decimal d, int decimals, MidpointRounding mode);
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
Parametri
- d
- Decimal
Numero decimale da arrotondare.
- decimals
- Int32
Numero di cifre decimali significative (precisione) nel valore restituito.
- mode
- MidpointRounding
Uno dei valori di enumerazione che specifica la strategia di arrotondamento da utilizzare.
Valori restituiti
Numero arrotondato d all'uso della mode strategia di arrotondamento e con precisione di decimals. Se la precisione di è minore di ddecimals, d viene restituita invariata.
Implementazioni
Eccezioni
decimals è minore di 0 o maggiore di 28.
mode non è un MidpointRounding valore.
Il risultato non rientra nell'intervallo di un Decimal oggetto .
Esempio
Nell'esempio seguente viene illustrato come utilizzare il Round(Decimal, Int32, MidpointRounding) metodo con l'enumerazione 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)
'
Commenti
Il decimals parametro specifica il numero di posizioni decimali significative nel valore restituito ed è compreso tra 0 e 28. Se decimals è zero, viene restituito un numero intero.
Se si specifica ToEven o AwayFromZero per il mode parametro, tali strategie di arrotondamento vengono applicate solo per i valori del punto medio, ovvero i valori la cui cifra minima significativa è 5.
Vedi anche
Si applica a
Round(Decimal, MidpointRounding)
- Origine:
- Decimal.cs
- Origine:
- Decimal.cs
- Origine:
- Decimal.cs
- Origine:
- Decimal.cs
- Origine:
- Decimal.cs
Arrotonda un valore decimale a un numero intero utilizzando la strategia di arrotondamento specificata.
public:
static System::Decimal Round(System::Decimal d, MidpointRounding mode) = System::Numerics::IFloatingPoint<System::Decimal>::Round;
public:
static System::Decimal Round(System::Decimal d, MidpointRounding mode);
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
Parametri
- d
- Decimal
Numero decimale da arrotondare.
- mode
- MidpointRounding
Uno dei valori di enumerazione che specifica la strategia di arrotondamento da utilizzare.
Valori restituiti
Intero d arrotondato all'utilizzo della mode strategia di arrotondamento.
Implementazioni
Eccezioni
mode non è un MidpointRounding valore.
Il risultato non rientra nell'intervallo di un Decimal oggetto .
Esempio
Nell'esempio seguente vengono visualizzati i valori restituiti dal Round(Decimal, MidpointRounding) metodo con argomenti diversi mode .
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
Commenti
Se si specifica ToEven o AwayFromZero per il mode parametro, tali strategie di arrotondamento vengono applicate solo per i valori del punto medio, ovvero i valori la cui cifra minima significativa è 5.
Vedi anche
Si applica a
Round(Decimal)
- Origine:
- Decimal.cs
- Origine:
- Decimal.cs
- Origine:
- Decimal.cs
- Origine:
- Decimal.cs
- Origine:
- Decimal.cs
Arrotonda un valore decimale all'intero più vicino.
public:
static System::Decimal Round(System::Decimal d) = System::Numerics::IFloatingPoint<System::Decimal>::Round;
public:
static System::Decimal Round(System::Decimal d);
public static decimal Round(decimal d);
static member Round : decimal -> decimal
Public Shared Function Round (d As Decimal) As Decimal
Parametri
- d
- Decimal
Numero decimale da arrotondare.
Valori restituiti
Intero più vicino al d parametro . Se d è a metà tra due interi, uno dei quali è pari e l'altro dispari, viene restituito il numero pari.
Implementazioni
Eccezioni
Il risultato non rientra nell'intervallo di un Decimal valore.
Esempio
Nell'esempio seguente viene arrotondato un intervallo di Decimal valori compreso tra 100 e 102 all'intero più vicino. Poiché il metodo utilizza l'arrotondamento del banchiere, 100,5 round a 100 e 101,5 round a 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 Run()
For value As Decimal = 100D To 102D Step 0.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
Commenti
Il comportamento di questo metodo segue IEEE Standard 754, sezione 4. Questo tipo di arrotondamento è talvolta chiamato metà rotonda per persino o rounding del banchiere. Riduce al minimo gli errori di arrotondamento risultanti dall'arrotondamento costante di un valore del punto intermedio in una singola direzione. Equivale a chiamare il Round(Decimal, MidpointRounding) metodo con un mode argomento di MidpointRounding.ToEven.
Vedi anche
Si applica a
Round(Decimal, Int32)
- Origine:
- Decimal.cs
- Origine:
- Decimal.cs
- Origine:
- Decimal.cs
- Origine:
- Decimal.cs
- Origine:
- Decimal.cs
Arrotonda un Decimal valore a un numero specificato di posizioni decimali.
public:
static System::Decimal Round(System::Decimal d, int decimals) = System::Numerics::IFloatingPoint<System::Decimal>::Round;
public:
static System::Decimal Round(System::Decimal d, int decimals);
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
Parametri
- d
- Decimal
Numero decimale da arrotondare.
- decimals
- Int32
Valore compreso tra 0 e 28 che specifica il numero di posizioni decimali da arrotondare.
Valori restituiti
Numero decimale equivalente a arrotondato alle ddecimals posizioni decimali.
Implementazioni
Eccezioni
decimals non è un valore compreso tra 0 e 28.
Esempio
Nell'esempio seguente vengono arrotondati diversi Decimal valori a un numero specificato di posizioni decimali usando il Round metodo .
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 Example12
Public Sub Run()
' 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
Commenti
Questo metodo equivale a chiamare il Round(Decimal, Int32, MidpointRounding) metodo con un mode argomento di MidpointRounding.ToEven. Quando d è esattamente a metà strada tra due valori arrotondati, il risultato è il valore arrotondato con una cifra pari nella posizione decimale all'estrema destra. Ad esempio, se arrotondato a due decimali, il valore 2,345 diventa 2,34 e il valore 2,355 diventa 2,36. Questo processo è noto come arrotondamento verso anche, o l'arrotondamento del banchiere. Riduce al minimo gli errori di arrotondamento risultanti dall'arrotondamento costante di un valore del punto intermedio in una singola direzione.