Decimal.Round 메서드

정의

값을 가장 가까운 정수 또는 지정된 소수 자릿수로 반올림합니다.

오버로드

Name Description
Round(Decimal, Int32, MidpointRounding)

지정된 반올림 전략을 사용하여 소수 값을 지정된 전체 자릿수로 반올림합니다.

Round(Decimal, MidpointRounding)

지정된 반올림 전략을 사용하여 10진수 값을 정수로 반올림합니다.

Round(Decimal)

10진수 값을 가장 가까운 정수로 반올림합니다.

Round(Decimal, Int32)

Decimal 값을 지정된 소수 자릿수로 반올림합니다.

Round(Decimal, Int32, MidpointRounding)

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

지정된 반올림 전략을 사용하여 소수 값을 지정된 전체 자릿수로 반올림합니다.

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

매개 변수

d
Decimal

반올림할 10진수입니다.

decimals
Int32

반환 값의 유효 소수 자릿수(전체 자릿수)입니다.

mode
MidpointRounding

사용할 반올림 전략을 지정하는 열거형 값 중 하나입니다.

반품

반올림 전략을 사용하고 d 전체 mode자릿수로 반올림되는 숫자 decimals 입니다. 전체 자릿수 d 가 보다 decimalsd 작으면 변경되지 않은 상태로 반환됩니다.

구현

예외

decimals 가 0보다 작거나 28보다 큰 경우

mode가 값이 아닌 경우 MidpointRounding

결과는 개체의 범위를 벗어났습니다 Decimal .

예제

다음 예제에서는 열거형을 사용 하 여 메서드를 Round(Decimal, Int32, MidpointRounding) 사용 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)
'

설명

매개 변수는 decimals 반환 값의 소수 자릿수를 지정하고 범위는 0에서 28까지입니다. 0이면 decimals 정수가 반환됩니다.

매개 변수를 ToEven 지정 AwayFromZero 하거나 mode 매개 변수를 지정하는 경우 이러한 반올림 전략은 중간점 값, 즉 최소 유효 자릿수가 5인 값에만 적용됩니다.

추가 정보

적용 대상

Round(Decimal, MidpointRounding)

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

지정된 반올림 전략을 사용하여 10진수 값을 정수로 반올림합니다.

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

매개 변수

d
Decimal

반올림할 10진수입니다.

mode
MidpointRounding

사용할 반올림 전략을 지정하는 열거형 값 중 하나입니다.

반품

반올림 전략을 사용하도록 반올림되는 정 dmode 입니다.

구현

예외

mode가 값이 아닌 경우 MidpointRounding

결과는 개체의 범위를 벗어났습니다 Decimal .

예제

다음 예제에서는 다른 Round(Decimal, MidpointRounding) 인수를 사용 하는 메서드에 의해 반환 된 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

설명

매개 변수를 ToEven 지정 AwayFromZero 하거나 mode 매개 변수를 지정하는 경우 이러한 반올림 전략은 중간점 값, 즉 최소 유효 자릿수가 5인 값에만 적용됩니다.

추가 정보

적용 대상

Round(Decimal)

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

10진수 값을 가장 가까운 정수로 반올림합니다.

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

매개 변수

d
Decimal

반올림할 10진수입니다.

반품

매개 변수에 가장 가까운 정수 d 입니다. 두 정수 중 하나가 짝수이고 다른 하나는 홀수인 경우 d 짝수 값이 반환됩니다.

구현

예외

결과는 값 범위를 벗어났습니다 Decimal .

예제

다음 예제에서는 100에서 102 사이의 값 범위를 Decimal 가장 가까운 정수로 반올림합니다. 이 방법은 은행의 반올림을 사용하므로 100.5는 100으로 반올림되고 101.5는 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

설명

이 메서드의 동작은 IEEE Standard 754, 섹션 4를 따릅니다. 이러한 종류의 반올림은 때로는 짝수 또는 은행의 반올림에 라운드 반이라고합니다. 단일 방향으로 중간점 값을 일관되게 반올림하여 발생하는 반올림 오류를 최소화합니다. 인수를 사용하여 메서드 Round(Decimal, MidpointRounding)mode를 호출하는 MidpointRounding.ToEven 것과 같습니다.

추가 정보

적용 대상

Round(Decimal, Int32)

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

Decimal 값을 지정된 소수 자릿수로 반올림합니다.

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

매개 변수

d
Decimal

반올림할 10진수입니다.

decimals
Int32

반올림할 소수 자릿수를 지정하는 0에서 28까지의 값입니다.

반품

소수 자릿수로 d 반올림된 decimals 소수에 해당하는 10진수입니다.

구현

예외

decimals 가 0에서 28까지의 값이 아닌 경우

예제

다음은 메서드를 사용하여 Decimal 여러 Round 값을 지정된 소수 자릿수로 반올림하는 예제입니다.

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

설명

이 메서드는 인수Round(Decimal, Int32, MidpointRounding)를 사용하여 메서드를 mode 호출하는 MidpointRounding.ToEven 것과 같습니다. 두 반올림된 값 사이의 정확히 절반인 경우 d 결과는 맨 오른쪽 소수 위치에 짝수 자릿수가 있는 반올림된 값입니다. 예를 들어 20진수로 반올림하면 값 2.345는 2.34가 되고 값 2.355는 2.36이 됩니다. 이 프로세스는 짝수 또는 은행원의 반올림으로 알려져 있습니다. 단일 방향으로 중간점 값을 일관되게 반올림하여 발생하는 반올림 오류를 최소화합니다.

추가 정보

적용 대상